有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java自定义Jersey参数解组

我收到一个字符串行作为查询参数,如

parameter=123,456,789

我想直接在我的控制器中获得List<Integer>。大概是这样的:

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

除了自定义提供程序和其他类型外,可以执行哪些操作:

https://stackoverflow.com/a/6124014

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


共 (1) 个答案

  1. # 1 楼答案

    没有内置的机制来实现这一点。您需要自己在方法或提供程序中拆分该字符串,或者在您自己的对象中拆分该字符串,该对象具有带字符串参数的构造函数,例如:

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

    其中MyList可能是:

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

    然后用你的方法得到我的名单