Custom Jersey parameter unmarshalling

扶醉桌前 提交于 2019-12-24 13:52:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23541854/custom-jersey-parameter-unmarshalling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!