Json Spring中的Jackson对象映射器不工作
我有一个SpringMVC应用程序,它接收来自Javascript前端的JSON。 使用Jackson 2,自定义对象映射器仅将ACCEPT_SINGLE_VALUE_AS_ARRAY设置为true。 下面是我的代码片段 启用MVC Java配置:Json Spring中的Jackson对象映射器不工作,json,spring,spring-mvc,jackson,Json,Spring,Spring Mvc,Jackson,我有一个SpringMVC应用程序,它接收来自Javascript前端的JSON。 使用Jackson 2,自定义对象映射器仅将ACCEPT_SINGLE_VALUE_AS_ARRAY设置为true。 下面是我的代码片段 启用MVC Java配置: @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configur
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(0, jackson2Converter());
}
@Primary
@Bean
public MappingJackson2HttpMessageConverter jackson2Converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(objectMapper());
return converter;
}
@Primary
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
return objectMapper;
}
}
POJO:
发布的JSON对象:mydata:{“dstState”:“NV”,“dstCities”:“拉斯维加斯”}
收到邮件后,出现错误:
Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: java.io.PushbackInputStream@44733b90; line: 1, column: 90] (through reference chain: com.*.Shipment["dstCities"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
请指出我这里缺少的任何内容您输入的json数据格式不正确
发货
Json表示:
是Json对象shipping
也是Json对象String-dstState
是Json数组List
装运
类的正确Json数据格式如下
[{"dstState":"NV" , "dstCities": ["Las Vegas"]}]
或
显示将发送JSON对象的其余部分:mydata:{“dstState”:“NV”,“dstCities”:“Las Vegas”}$.ajax({url:…/home”,键入:“POST”,data:JSON.stringify((“#logdForm”).serializeArray()),contentType:“application/JSON”,数据类型:'json',…这对您有帮助吗?@JamesJithin指出的问题是针对javax.ws.rs和Codehaus-jackson的,我没有使用提供者注释:(
$.ajax({ url: ".../home", type: "POST", data: JSON.stringify(("#shipForm").serializeArray()), contentType: "application/json", dataType: 'json', ....
Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: java.io.PushbackInputStream@44733b90; line: 1, column: 90] (through reference chain: com.*.Shipment["dstCities"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
[{"dstState":"NV" , "dstCities": ["Las Vegas"]}]
[{"dstState":"NV" , "dstCities": ["Las Vegas", "Las Not Vegas"]}]