自定义Jersey参数解组 [英] Custom Jersey parameter unmarshalling

查看:102
本文介绍了自定义Jersey参数解组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到像

parameter=123,456,789

我想直接在我的控制器中获得List<Integer>.像这样:

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 ?

更新:自定义批注解决了 http://avianey. blogspot.de/2011/12/exception-mapping-jersey.html

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

MyList可能在其中:

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.

这篇关于自定义Jersey参数解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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