我有一个像http://www.localhost.com/service/resource?status=ACTIVE,PASSIVE这样的休息网址,我有这样的方法
@GET
public Resource resource(@QueryParam("status") Collection<STATUS> statusList){
}
Run Code Online (Sandbox Code Playgroud)
哪里STATUS是枚举?
public enum STATUS{
ACTIVE,PASSIVE,DISABLED
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有一种方法可以将查询参数状态自动转换为泽西中的枚举类型STATUS列表,还是我必须为此编写自己的提供程序?
Eug*_*vas 49
在JSR 311 API中,带@QueryParam注释的类型必须:
String参数的构造函数valueOf或fromString接受单个String参数的静态方法(例如,参见Integer.valueOf(String))List<T>,Set<T>或者SortedSet<T>,T满足2或3以上.生成的集合是只读的.对于你的情况,我会通过将枚举包装在一个简单的类中来使用第二个选项:
public class StatusList {
private List<STATUS> statusList;
public StatusList(String toParse) {
//code to split the parameter into a list of statuses
}
public List<STATUS> getStatusList() {
return this.statusList;
}
}
Run Code Online (Sandbox Code Playgroud)
然后将您的方法更改为:
@GET
public Resource resource(@QueryParam("status") StatusList statusList){
}
Run Code Online (Sandbox Code Playgroud)
小智 6
试试这个:
http://www.localhost.com/service/resource?status=ACTIVE&status=PASSIVE
Run Code Online (Sandbox Code Playgroud)
@QueryParam("status") List<STATUS> statusList
Run Code Online (Sandbox Code Playgroud)
将此方法添加到您的枚举中:
public static YourEnum fromString(final String s) {
return YourEnum.valueOf(s);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27990 次 |
| 最近记录: |