如何在REST服务的get方法的URL中传递逗号分隔的参数 [英] How to pass comma separated parameters in a url for the get method of rest service

查看:1217
本文介绍了如何在REST服务的get方法的URL中传递逗号分隔的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的网络服务

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

如果我从测试类中调用此Web服务,则它可以正常工作;但是如果我通过浏览器调用它,则会收到消息,因为找不到该服务.

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

如果我使用url作为

http://localhost:8080/WebApp /services/seating/mylogin/fly/1/0/0/month

并相应地更改服务中的路径,它可以正常工作,但是要求是使用逗号而不是斜杠.有什么方法可以在网址中使用带有逗号分隔参数的Web服务?

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?

推荐答案

在您的示例中,您使用PathSegment表示整个路径段,在您的情况下为"1,0,0",因此无法解析作为整数.

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.

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

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

响应

.../some-resource/parameter1/july/1,2,3/month

.../some-resource/parameter1/july/1,2,3/month

参数1:1、2、3

这篇关于如何在REST服务的get方法的URL中传递逗号分隔的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆