如何使用Jackson将JSON存储到POJO中

时间:2013-11-07 09:51:04

标签: json rest

我正在开发一个模块,我正在使用rest服务来获取数据。我没有得到如何使用杰克逊存储JSON并存储它也有Queryparam。任何帮助都非常感谢,因为我是新手。我正在尝试在extjs infinte网格中进行服务器端过滤,该网格正在向休息服务发送以下请求。

首次加载页面时,会发送:

http://myhost/mycontext/rest/populateGrid?_dc=9999999999999&page=1&start=0&limit=500

当您选择名称和地点的过滤器时,它会发送:

http://myhost/mycontext/rest/populateGrid?_dc=9999999999999&filter=[{"type":"string","value":"Tom","field":"name"},{"type":"string","value":"London","field":"Location"}]&page=1&start=0&limit=500

我正在尝试将其保存在POJO中,然后将其发送到数据库以检索数据。对于休息方面,我写了这样的话:

@Provider
@Path("/rest")
public interface restAccessPoint {
@GET
@Path("/populateGrid")
@Produces({MediaType.APPLICATION_JSON})
public Response getallGridData(FilterJsonToJava filterparam,@QueryParam("page") String page,@QueryParam("start") String start,@QueryParam("limit") String limit);
}

public class FilterJsonToJava {
@JsonProperty(value ="filter")
private List<Filter> data;
.. getter and setter below
}

public class Filter {
@JsonProperty("type")
private String type;
@JsonProperty("value")
private String value;
@JsonProperty("field")
private String field;
...getter and setters below
}

我收到以下错误:

The following warnings have been detected with resource and/or provider classes: WARNING: A HTTP GET method, public abstract javax.ws.rs.core.Response com.xx.xx.xx.xxxxx (com.xx.xx.xx.xx.json.FilterJsonToJava ,java.lang.String,java.lang.String,java.lang.String), should not consume any entity.  

com.xx.xx.xx.xx.json.FilterJsonToJava, and Java type class com.xx.xx.xx.FilterJsonToJava, and MIME media type application/octet-stream was not found  

[11/6/13 17:46:54:065] 0000001c ContainerRequ E   The registered message body readers compatible with the MIME media type are:

application/octet-stream  
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider  com.sun.jersey.core.impl.provider.entity.FileProvider   com.sun.jersey.core.impl.provider.entity.InputStreamProvider   com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.RenderedImageProvider */* -> com.sun.jersey.core.impl.provider.entity.FormProvider ...

1 个答案:

答案 0 :(得分:0)

你应该尝试这样做:

Response getallGridData(@QueryParam("filter") String filterparam, ...) { 

ObjectMapper mapper = new ObjectMapper();
Filter yourObject = mapper.readValue(filterparam, Filter.class);

}

这是方法,因为您的有效负载在查询参数中。当存在有效负载时,对象以POST请求注入。