记录碰到的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"])

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

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

为啥折腾了很久呢,用的公司的一个框架,看不到请求之后的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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值