有人可以帮我弄清楚需要添加什么吗?
JSON:
{"value":{"keyword":"better","correct":"","page":0,"size":10,"cost":51,"total":1107}}
对象类
@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;
仍然可以获得servlet appServlet的Servlet.service()抛出异常
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 无法识别的字段“值”(),未标记为可忽略的“
答案 0 :(得分:9)
尝试将此添加到您的mapper配置
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
如果您使用RestTemplate,则需要配置基础杰克逊映射器。您可以通过配置映射器并在转换器中进行设置来完成此操作。请参阅下面的代码。
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);