Replace ampersand in URL with "and" - java

I'm a bit new to Spring and trying to replace my parameters to pass as key:value pairs and also replace an ampersand with a literal "and" to avoid issues when hitting the screen. Example:
I want to go from: mySite/myPage.html?myId=154933680&myRequest='QT'
to have a separate method/entrypoint that will work like this:
myPage?search=myId=154735535 and myRequest="RT"
Now, my current header looks like this:
#RequestMapping(value = "myPage.html", method = RequestMethod.GET) public ModelAndView getSite(HttpServletRequest request, #RequestParam String myId, #RequestParam String myRequest)
I'm trying to do something like this instead, but its not working:
#RequestMapping(value = "myPage?search=", method = RequestMethod.GET) public ModelAndView mySearch(HttpServletRequest request, #RequestParam String myId, #RequestParam String myRequest) {
But I would like to pass in them as key:value params rather than straight Strings as well. I'm a little all over the place here, I guess my main question would be with what I have above, how do I swap my URL to not string the parameters together with a "&" but to use "and" instead?

Related

How to pass forward slash(/) in path variable of uri?

I have a rest api implementation as below -
#RequestMapping(value = "/getAllProductsByCategory/{category}/{pageNo}/{numberOfProducts}", method = RequestMethod.GET)
public List<ProductProperties> getAllProdutsByCategory(#PathVariable("category") String categoryID, #PathVariable("pageNo") int pageNo,
#PathVariable("numberOfProducts") int numberOfProducts) {
return productService.getProductsByCategory(categoryID, pageNo, numberOfProducts);
}
Now i want to test this method where category variable should be like "men/clothing/jeans". I tried to use escape character %2F to replace forward slash, but had no luck. Is there any way to pass forward slash in uri ? I tried to google same question but didn't find any satisfactory answer.

Spring Request Mapping based on any/no query string

When using Spring MVC, is there a way to create two entry points by whether or not any query string has been supplied in the request.
Something like below where * is a wildcard?
#RequestMapping(value = "/page", method = RequestMethod.GET, params = {"*"})
public String getResourceWithQuery(...)
#RequestMapping(value = "/page", method = RequestMethod.GET, params = {"!*"})
public String getResourceWithoutQuery(...)
Is this possible with Spring?
Edit: To be clear, I'm not looking for a particular query parameter, I'm looking to separate the methods by the existence of any query string being present at all.
The fall back is to have one method and then check in code for query parameters and split accordingly. Having a filter method like this is messy and I'd prefer not to have to do this. Unfortunately the splitting functionality by query pattern is common in my code as it is required by the business.
One end point is enough.
You can set default value for request parameter(or query string), this will make request parameter optional.
As per java doc,
defaultValue:
public abstract String defaultValue The default value to use as a fallback > when the request parameter is not provided or has
an empty value. Supplying a default value implicitly sets required()
to false.
For example,
public String doSomething(#RequestParam(value = "name", defaultValue = "anonymous") final String name) {
Your are trying to map the request URI which having the query string or not.you are using the params in #RequestMapping which actually use for narrow the Request matching.Read below link
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params--
.By using below code you can accept anything after ~/page/ URI.I hope this will help
#RequestMapping(value = "/page/**", method = RequestMethod.GET)
public String getResourceWithQuery(...)

How to create REST API with optional parameters?

I need to implement an API with these path params.
#Path("/job/{param1}/{optional1}/{optional2}/{param2}")
Can the second and third params by optional? So the client need not pass these, but have to pass the first and last.
If this is not possible, then is it recommended to rearrange the params in this way?
#Path("/job/{param1}/{param2}/{optional1}/{optional2}")
How to provide the optional params?
It might be easier to turn the optional path parameters into query parameters. You can then use #DefaultValue if you need it:
#GET #Path("/job/{param1}/{param2}")
public Response method(#PathParam("param1") String param1,
#PathParam("param2") String param2,
#QueryParam("optional1") String optional1,
#QueryParam("optional2") #DefaultValue("default") String optional2) {
...
}
You can then call it using /job/one/two?optional1=test passing only the optional parameters you need.
You can match the entire path ending in the REST request
#Path("/location/{locationId}{path:.*}")
public Response getLocation(
#PathParam("locationId") int locationId,
#PathParam("path") String path) {
//your code
}
Now the path variable contain entire path after location/{locationId}
You can also use a regular expressions to make the path optional.
#Path("/user/{id}{format:(/format/[^/]+?)?}{encoding:(/encoding/[^/]+?)?}")
public Response getUser(
#PathParam("id") int id,
#PathParam("format") String format,
#PathParam("encoding") String encoding) {
//your code
}
Now if you format and encoding will be optional. You do not give any value they will be empty.
Rearrange the params and try the following:
#Path("/job/{param1}/{param2}{optional1 : (/optional1)?}{optional2 : (/optional2)?}")
public Response myMethod(#PathParam("param1") String param1,
#PathParam("param2") String param2,
#PathParam("optional1") String optional1,
#PathParam("optional2") String optional2) {
...
}
to make request parameter optional set #requestparam to false in controller class
(#RequestParam(required=false)

How can you print out the values of undeclared HTML form values from a Spring (3) controller action method?

If my HTML form contains two form inputs (input1 and input2), I could access them like this:
#RequestMapping(value = "/foo", method = RequestMethod.POST)
public String foo(HttpServletRequest request, ModelMap modelMap,
#RequestParam(value = "input1") String input1,
#RequestParam(value = "input2") String input2)
{
log.write("input1=" + input1);
log.write("input2=" + input2);
return "redirect:/foo/";
}
But what if I have other form elements on the HTML page that I don't know about?
How can I print out the values of form elements that I have not declared in the action method with a #RequestParam annotation?
using HttpServletRequest - request.getParameterNames() this will get all the submitted parameters.

Why does #RequestParameter String someValue in Spring 3 return 2x values?

Let's say I have this method in my controller:
#RequestMapping(value="/home", method=RequestMethod.GET)
public void captcha(#RequestParam String someValue, HttpServletResponse response)
{
System.out.println(someValue);
}
Why does the result of this request:
http://something/home?someValue=testvalue123
return this?
testvalue123,testvalue123
Using an Int only gives a single value as expected, but not String. Getting the parameter directly from the request-object also gives a single value.
Turns out there was a filter applied from some other library that incorrectly added the same request parameter a second time!
does adding "test" value to the #RequestParam Annotation help:
#RequestParam("test") String someValue

Categories

Resources