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?

4

3 回答 3

1

在您的示例中,您使用PathSegment的代表整个路径段,在您的情况下为“1,0,0”,因此不能解析为整数。

如果您使用int而不是PathSegment,则将按照您的预期提取值,并且方法主体会更加简洁。

以下代码对我来说很好:

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

回应

.../一些资源/参数1/七月/1,2,3/月

参数1:1、2、3

于 2013-12-17T13:14:51.097 回答
1

对我来说,用逗号分隔多个参数没有问题,即使它们是路径的一部分而不是查询参数。我测试了它,它确实有效。

int实际上,如果您不需要检查这些参数的正确性,您甚至可以直接绑定到。我确实使用@PathVariable了这些绑定。

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

编辑:

至于我测试的代码是这样的:

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

对于第一种方法,正确的视图位于 /seating/month.jsp。

或者,返回由 3 个参数组成的实体并生成 json 或 xml 也没有问题。

于 2012-04-25T11:48:48.967 回答
0

URL 参数不是逗号分隔的,它们是 '&' 分隔的。所以你要做的是'&' - 将它们分开。

于 2012-04-25T10:48:32.467 回答