Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON : Unrecognized field "value" (<objectClass>), not marked as ignorable

Tags:

java

json

jackson

Can someone help me to figure out what's need to be added?

JSON :

{"value":{"keyword":"better","correct":"","page":0,"size":10,"cost":51,"total":1107}}

Object class

@JsonAutoDetect
@JsonSerialize(include = Inclusion.NON_NULL)
@JsonRootName(value = "value")    
public class Response {

private int page;
private int size;
private int total;
private int cost;
private int result;

private String keyword;
private String correct;

Still it gets the "Servlet.service() for servlet appServlet threw exception

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "value" (), not marked as ignorable"

like image 801
iamtheexp01 Avatar asked Feb 20 '12 10:02

iamtheexp01


1 Answers

Try adding this to your mapper config

mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

If you use RestTemplate you will need to configure the underlying jackson mapper. You can do this by configuring your mapper and setting it in the converter. See code below.

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);


MappingJacksonHttpMessageConverter messageConverter = new MappingJacksonHttpMessageConverter();
messageConverter.setObjectMapper(mapper);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);

See here for more details: https://jira.springsource.org/browse/ANDROID-45

like image 191
Usman Ismail Avatar answered Sep 23 '22 01:09

Usman Ismail