当前位置:   article > 正文

记录碰到的json转换异常

记录碰到的json转换异常

先把报错信息贴出来:

org.springframework.web.client.RestClientException: Error while extracting response for type [com.mi.info.comb.common.x5protocol.core.dto.X5Response<com.mi.info.miim.fs.service.x5.dto.customer.OwnerPageDTO>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "country" (class com.mi.info.miim.fs.service.x5.dto.customer.OwnerInfoDTO), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "country" (class com.mi.info.miim.fs.service.x5.dto.customer.OwnerInfoDTO), not marked as ignorable (5 known properties: "status", "veCode", "veComName", "veComNameEn", "veComShortName"])
 at [Source: (PushbackInputStream); line: 1, column: 112] (through reference chain: com.mi.info.comb.common.x5protocol.core.dto.X5Response["body"]->com.mi.info.miim.fs.service.x5.dto.customer.OwnerPageDTO["records"]->java.util.ArrayList[0]->com.mi.info.miim.fs.service.x5.dto.customer.OwnerInfoDTO["country"])

  • 1
  • 2
  • 3

从下面这行错误信息大致可以看出,有个字段country没找到,导致报错了

ested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "country" (class com.mi.info.miim.fs.service.x5.dto.customer.OwnerInfoDTO)
  • 1

为啥折腾了很久呢,用的公司的一个框架,看不到请求之后的response中的内容,后来尝试增加了配置,配置主要是用于:json转对象的时候,如果字段找不到,就不赋值,这样不存在的字段直接跳过就好了。配置如下:

package com.*;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        converter.setObjectMapper(objectMapper);
        return converter;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

问题得到解决,记录一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/733851
推荐阅读
相关标签
  

闽ICP备14008679号