在REST Webservice中接受逗号分隔值 [英] Accept comma separated value in REST Webservice
问题描述
我试图在REST URI中接收一个String列表作为逗号分隔值(示例:
I am trying to receive a list of String as comma separated value in the REST URI ( sample :
http://localhost:8080/com.vogella.jersey.first/rest/todo/test/1/abc,test
,其中abc和test是传入的逗号分隔值。)
, where abc and test are the comma separated values passed in).
目前我将此值作为字符串获取,然后将其拆分以获取各个值。
当前代码:
Currently I am getting this value as string and then splitting it to get the individual values. Current code :
@Path("/todo")
public class TodoResource {
// This method is called if XMLis request
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/test/{id: .*}/{name: .*}")
public Todo getXML(@PathParam("id") String id,
@PathParam("name") String name) {
Todo todo = new Todo();
todo.setSummary("This is my first todo, id received is : " + id
+ "name is : " + Arrays.asList(name.split("\\s*,\\s*")));
todo.setDescription("This is my first todo");
TodoTest todoTest = new TodoTest();
todoTest.setDescription("abc");
todoTest.setSummary("xyz");
todo.setTodoTest(todoTest);
return todo;
}
}
有没有更好的方法来实现同样的目标?
Is there any better method to achieve the same?
推荐答案
我不确定您要使用的服务实现了什么,但是,使用查询参数获取可能会更好单个参数的多个值。请考虑以下网址。
I am not sure what you are trying to achieve with your service, however, it may be better to use query parameters to get multiple values for a single parameter. Consider the below URL.
http://localhost:8080/rest/todos?name=name1&name=name2&name=name3
这是REST服务的代码片段。
And here is the code snippet for the REST service.
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/todos")
public Response get(@QueryParam("name") List<String> names) {
// do whatever you need to do with the names
return Response.ok().build();
}
这篇关于在REST Webservice中接受逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!