I receive as a query parameter a String line like

parameter=123,456,789

What I want to obtain is List<Integer> directly in my controller. Something like this:

@GET
@Path(REST_PATH)
@Produces(MediaType.APPLICATION_JSON)
public Response getSomeStuff(@MagicalThing("parameter") List<Integer> requiredList)

What can be done except for custom provider and an additional type:

https://stackoverflow.com/a/6124014?

Update: custom annotation solves the puzzle http://avianey.blogspot.de/2011/12/exception-mapping-jersey.html

有帮助吗?

解决方案

There is no build in mechanism to do this. You will need to split that string by yourself either in your method or in provider, or in your own object which has a constructor with string parameter, for example:

public Response getSomeStuff(@QueryParam("parameter") MyList requiredList) {
    List<String> list = requiredList.getList();
}

where MyList may be:

 public class MyList {
    List<String>  list;
    public MyList(Srting parameter) {
        list = new ArrayList<String>(parameter.split(","));    
    }

    public List<String> getList() {
        return list;   
    }
}

And then obtain my list in your method.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top