简体   繁体   中英

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

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM