11

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?

Richard
  • 3,316
  • 30
  • 41
Ravi Mishra
  • 181
  • 1
  • 3
  • 8
  • 1
    +1 for a well written question. They're so hard to come by these days on SO. – adarshr Apr 25 '12 at 10:34
  • 1
    Why are you using the URL path to pass parameters to your service instead of sending them as standard GET params? For instance, I would deploy the service to `/WebApp/services/seating`, and then have clients make calls to URL's like `/WebApp/services/seating?param1=mylogin&param2=fly&param3=1&param4=0...`. – aroth Apr 25 '12 at 10:39
  • I cannot make calls using? then params, all the urls are in js file and dynamically get called based on the request from the UI, In w3 recommendations I saw browsers drop the parameters if comma is encountered and looks for resource before comma, I guess that is causing the issue, my test class invokes the service without any issues and returns the result – Ravi Mishra Apr 25 '12 at 11:56
  • @RaviMishra so which answer helped you? can you mark it as "This answer is useful" by upvoting it. – Suresh Sep 10 '19 at 07:05

3 Answers3

1

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);
}

Edit:

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.

Alex
  • 25,147
  • 6
  • 59
  • 55
  • I did the same using @PathVariable but didn't worked for me, my bad luck, did you used url like this http://localhost:8080/WebApp/services/seating/mylogin/july/1,0,0/month and tried accessing the service using browser? – Ravi Mishra Apr 25 '12 at 12:17
  • Yes, I modified an application that I use in local, and tried both url: `localhost/webapp/seating/1,2,3` and `localhost/webapp/seating/1,2,3/month`. Do you want me to update the answer to include my test code? – Alex Apr 25 '12 at 12:26
  • Please do, that would be very helpful – Ravi Mishra Apr 25 '12 at 12:28
  • Thanks a lot Alex, I found the issue and fixed it, actually it was url encoding issue from the UI of the application, the url was not encoded properly with corresponding ascii value of the comma. – Ravi Mishra Apr 27 '12 at 08:36
1

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

Jonas
  • 2,910
  • 2
  • 26
  • 36
0

URL parameters are not comma-separated, they are '&'-separated. So what you have to do is '&'-separate them.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • That's correct, except he doesn't even have a query-string yet. The OP is passing parameters as part of the URL path itself. – aroth Apr 25 '12 at 11:27
  • Thanks for the reply but, I cannot use & here, I don't have even the frontend/UI of the application, all I have is the url and browser which I can use to hit the service – Ravi Mishra Apr 25 '12 at 11:51
  • @RaviMishra That doesn't make any sense to me. Your question talks about 'comma separated parameters in the URL'. If that isn't what it is really about, I suggest you edit your question accordingly. – user207421 Apr 25 '12 at 12:40
  • @EJP actually its not just about url, its about using url having comma separated parameters to call a webservice. The url belongs to a rest webservice – Ravi Mishra Apr 26 '12 at 05:41
  • @RaviMishra So make up your mind. If they are parameters to the URL they should be &-separated, no two ways about it. If they are *part of* a single parameter, the commas need to be URL-encoded with `java.net.URLEncoder`. You can't put just anything you like into a URL, there are rules about this. – user207421 Apr 26 '12 at 06:52
  • @EJP I understand that the parameters needs to be separated by & in url if it is a get method but, to decide on that is not on my hand, the requirement insists that the parameters needs to be separated by comma and they are different parameters so, I was just looking for any suggestions if it can be done. There is an answer in this page posted where this has been achieved. – Ravi Mishra Apr 26 '12 at 07:06
  • Hi EJP, you were right on encoding the url, the issue was with the url which was not encoded from the UI insread it was directly sent using comma separated parameters. It is fixed, Thanks a lot – Ravi Mishra Apr 27 '12 at 08:38