[英]Complex object as a queryparameter in in java rest application
I need to be able to query a resource with a filter with many parameters (all optionals) 我需要能够使用具有许多参数的过滤器查询资源(所有选项)
my url specification is something like this : 我的网址规范是这样的:
GET http://something/version/resource?f={"param1":"1","param2":"something else", "param3":"tomato"
I tried two approaches: 我尝试了两种方法:
@XmlRootElement
so I created a class filter and annotated it with the @XmlRootElement
but the parameters are not parsed into my class. @XmlRootElement
因此我创建了一个类过滤器并使用@XmlRootElement
对其进行了注释,但参数未被解析到我的类中。 @XmlRootElement
MyClassFilter{
String param1;
String param2;
..........
}
@BeanParam
so I removed the @XmlRootElement
annotation and I put a @QueryParam
annotation for every field in the class and in the resource method I put the @BeanParam
one. @BeanParam
所以我删除了@XmlRootElement
注释,我为类中的每个字段添加了@QueryParam
注释,在资源方法中我放了@BeanParam
。 MyClassFilter{
@QueryParam("param1")
String param1;
@QueryParam("param2")
String param2;
..........
}
I get null
objects with both methods. 我用这两种方法得到了
null
对象。 Could someone point me to the right direction? 有人能指出我正确的方向吗? I usually consume the services so I don't have much experience on the server side.
我通常使用服务,所以我在服务器端没有太多经验。
I solved my problem, in my get method I receive the filter as a string: 我解决了我的问题,在我的get方法中,我将过滤器作为字符串接收:
@QueryParam(value = "f") String f
and I parse it using the ObjectMapper: 我使用ObjectMapper解析它:
ObjectMapper mapper = new ObjectMapper();
EntityFilter filter = mapper.readValue(f, EntityFilter.class);
You can use ParamConverterProvider
. 您可以使用
ParamConverterProvider
。
@Provider
public class JSONParamConverterProvider implements ParamConverterProvider {
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if(rawType.equals(MyObject.class)){
return (ParamConverter<T>) new JSONParamConverter();
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.