I have a webservice like
@GET
@Produces("application/json")
@Path("{parameter1}/july/{param2},{param3},{param4}/month")
public Month getResult(@PathParam("parameter1") String parameter1, @PathParam("param2") PathSegment param2 , @PathParam("param3") PathSegment param3, @PathParam("param4") PathSegment param4) {
return action.getResult(parameter1, new Integer(param2.getPath()), new Integer(param3.getPath()), new Integer(param3.getPath()));
}
If I call this web service from my test class, it works fine; but if I call it through the browser, I get message as cannot find the service.
The url I am using through the browser is
http://localhost:8080/WebApp/services/seating/mylogin/july/1,0,0/month
if I use the url as
http://localhost:8080/WebApp/services/seating/mylogin/fly/1/0/0/month
and change the path in the service accordingly it works fine, but the requirement is to use comma instead of slash. Is there any way we can use the webservice with comma-separated parameters in the url?
We can create a parameterized URL that can be reused as required by providing value. How to put parameters in URL? A parameter is a key-value pair. A URL will have parameters whose value can be provided in multiple ways.
REST API Framework A set of parameters attached to the end of the URL is called Query Parameters. They are appended to the URL by adding ‘?’ at the end of the URL. In addition to that, they are followed immediately with a key-value pair (Query Parameter).
This means the comma has defined meaning at various parts in a URL, and if it is not being used in that context it needs to be percent-encoded. That said, the query parameter doesn't give the comma any special syntax, so in query parameters we probably shouldn't be encoding it.
How to pass Query Parameters in HTTP Get Request using Rest Assured? A set of parameters attached to the end of the URL is called Query Parameters. They are appended to the URL by adding '? ' at the end of the URL. In addition to that, they are followed immediately with a key-value pair ( Query Parameter ).
For me there is no problem with separating multiple parameters with a comma, even if these are part of the path instead of being query parameters. I tested it and it actually works.
Actually you can even directly bind to int
if you do not need to check for correctness of these parameters. I did use @PathVariable
for these binding.
@GET
@Produces("application/json")
@Path("{parameter1}/july/{param2},{param3},{param4}/month")
public Month getResult(@PathVariable("parameter1") String parameter1, @PathVariable("param2") int param2 , @PathVariable("param3") int param3, @PathVariable("param4") int param4) {
return action.getResult(parameter1, param2, param3,param3);
}
As for the code I tested this is it:
@Controller
public class InfoController {
@RequestMapping(method = RequestMethod.GET, value = "/seating/{param1},{param2},{param3}/month")
public String showMonthView(Model uiModel, @PathVariable("param1") int p1,
@PathVariable("param2") int p2, @PathVariable("param3") int p3,
HttpServletRequest httpServletRequest) {
LOG.debug(String.format("view:/seating/%d,%d,%d/month", p1, p2, p3));
uiModel.addAttribute("param1", p1);
uiModel.addAttribute("param2", p2);
uiModel.addAttribute("param3", p3);
return "month";
}
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/seating/{param1},{param2},{param3}/month", produces="application/json")
public Map<String, Integer> showMonthJson(@PathVariable("param1") final int p1,
@PathVariable("param2") final int p2, @PathVariable("param3") final int p3) {
LOG.debug(String.format("json:/seating/%d,%d,%d/month", p1, p2, p3));
Map<String, Integer> result = new HashMap<String, Integer>() {{
put("param1", p1);
put("param2", p2);
put("param3", p3);
}};
return result;
}
}
With a correct view located at /seating/month.jsp for the first method.
Alternatively, returning an entity composed of the 3 params and producing json or xml makes no problem either.
In your example you are using PathSegment
which represent the whole path segment, which in your case is "1,0,0" and thus cannot be parsed as integer.
If you would use int
instead of PathSegment
, the values would be extracted as you expect and the method body would be much more concise.
The following code worked fine for me:
@GET
@Path("{parameter1}/july/{param2},{param3},{param4}/month")
public String commaSeparatedValueDemo(@PathParam("parameter1") String parameter1, @PathParam("param2") int param2, @PathParam("param3") int param3, @PathParam("param4") int param4) {
return MessageFormat.format("{0}: {1}, {2}, {3}", parameter1, param2, param3, param4);
}
Response for
.../some-resource/parameter1/july/1,2,3/month
is
parameter1: 1, 2, 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With