Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Spring中的Jackson对象映射器不工作_Json_Spring_Spring Mvc_Jackson - Fatal编程技术网

Json Spring中的Jackson对象映射器不工作

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

我有一个SpringMVC应用程序,它接收来自Javascript前端的JSON。 使用Jackson 2,自定义对象映射器仅将ACCEPT_SINGLE_VALUE_AS_ARRAY设置为true。 下面是我的代码片段

启用MVC Java配置:

@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表示:

  • shipping
    是Json对象
  • String-dstState
    也是Json对象
  • List
    是Json数组
您的
装运
类的正确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"]}]