Java – Accept comma separated value in REST Webservice


I am trying to receive a list of String as comma separated value in the REST URI ( sample :


, 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 :

public class TodoResource {
// This method is called if XMLis request
@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();
    return todo;

Is there any better method to achieve the same?

Best Solution

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.


And here is the code snippet for the REST service.

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response get(@QueryParam("name") List<String> names) {

    // do whatever you need to do with the names

   return Response.ok().build();