记录碰到的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. 记录碰到json转换异常

    2024-06-16 23:46:02       30 阅读
  2. c# Xml 和 Json 转换方法记录

    2024-06-16 23:46:02       37 阅读
  3. proto与json互相转换

    2024-06-16 23:46:02       70 阅读
  4. 各个类型和Json类型相互转换

    2024-06-16 23:46:02       41 阅读
  5. MATLABmat文件转换json文件

    2024-06-16 23:46:02       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 23:46:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 23:46:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 23:46:02       87 阅读
  4. Python语言-面向对象

    2024-06-16 23:46:02       96 阅读

热门阅读

  1. 网络安全sql注入实战演示

    2024-06-16 23:46:02       28 阅读
  2. Python 调用 C 和 C 调用 Python 方法

    2024-06-16 23:46:02       33 阅读
  3. Optional详解和常用API

    2024-06-16 23:46:02       23 阅读
  4. ssh 两次跳转,通过跳板机直接登录设备

    2024-06-16 23:46:02       27 阅读
  5. 智谱API调用

    2024-06-16 23:46:02       33 阅读
  6. 对大数据的批量导入MySQL数据库

    2024-06-16 23:46:02       32 阅读
  7. 演绎推理三段论(大前提、小前提、结论)

    2024-06-16 23:46:02       32 阅读
  8. 杂谈-C和C++有什么不同

    2024-06-16 23:46:02       33 阅读
  9. 22.1 正则表达式-定义正则表达式、正则语法

    2024-06-16 23:46:02       24 阅读
  10. js中有哪些函数?

    2024-06-16 23:46:02       33 阅读
  11. Web前端开发12章:深入探索与实战解析

    2024-06-16 23:46:02       29 阅读