【BUG】已解决:JsonMappingException

已解决:JsonMappingException

欢迎来到英杰社区https://bbs.csdn.net/topics/617804998

概述:

没有getter方法的实体的序列化,并解决Jackson引发的JsonMappingException异常。

默认情况下,Jackson 2只会处理公有字段或具有公有getter方法的字段。如果实体的所有字段都是私有或包内可见的,序列化将会失败:

复制代码
public class MyDtoNoAccessors {
    String stringValue;
    int intValue;
    boolean booleanValue;

    public MyDtoNoAccessors() {
        super();
    }

    // no getters
}

@Test(expected = JsonMappingException.class)
public void givenObjectHasNoAccessors_whenSerializing_thenException() 
  throws JsonParseException, IOException {
    String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, notNullValue());
}

完整的异常如下:

复制代码
com.fasterxml.jackson.databind.JsonMappingException: 
No serializer found for class dtos.MyDtoNoAccessors 
and no properties discovered to create BeanSerializer 
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )

3.1. 全局自动检测任何可见性的字段

对于这个问题的一个解决方案是全局配置ObjectMapper,使其检测所有字段,不论其可见性:

复制代码
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

这将允许Jackson检测到没有getter方法的私有和包内可见字段,从而实现正确的序列化:

复制代码
@Test
public void givenObjectHasNoAccessors_whenSerializingWithAllFieldsDetected_thenNoException() 
  throws JsonParseException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
}

3.2. 在类级别控制字段可见性

Jackson 2还提供了另一种选择,即通过@JsonAutoDetect注解在类级别控制字段可见性:

复制代码
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class MyDtoNoAccessors { ... }

使用这个注解,这个特定类的序列化现在应该可以正常工作:

复制代码
@Test
public void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() 
  throws JsonParseException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
}

4. 在Jackson中禁用fail_on_empty_beans

在Jackson中,fail_on_empty_beans特性决定了在序列化过程中遇到空对象(没有属性)时是否抛出异常。默认情况下,Jackson会遇到空bean时抛出异常。

值得注意的是,fail_on_empty_beans特性默认启用,若要禁用它,我们需要明确设置为false。具体方法取决于我们的具体用例。

4.1. 使用ObjectMapper配置

可以直接在ObjectMapper上禁用fail_on_empty_beans

复制代码
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

通过这种方式配置ObjectMapper,我们告诉Jackson在序列化过程中遇到空bean时不抛出异常。

4.2. 使用Spring Boot

在Spring Boot中,我们可以在application.properties文件中设置以下属性以全局禁用fail_on_empty_beans

复制代码
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

这个属性可以在应用级别设置,以控制Jackson序列化在整个应用中的行为

【其他错误】

如果出现模块错误

python 复制代码
进入控制台输入:建议使用国内镜像源

pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple

我大致罗列了以下几种国内镜像源:

清华大学
https://pypi.tuna.tsinghua.edu.cn/simple
     
阿里云
https://mirrors.aliyun.com/pypi/simple/
     
豆瓣
https://pypi.douban.com/simple/
     
百度云
https://mirror.baidu.com/pypi/simple/
     
中科大
https://pypi.mirrors.ustc.edu.cn/simple/
     
华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
     
腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/
相关推荐
白日做梦Q4 分钟前
Anchor-free检测器全解析:CenterNet vs FCOS
python·深度学习·神经网络·目标检测·机器学习
喵手18 分钟前
Python爬虫实战:公共自行车站点智能采集系统 - 从零构建生产级爬虫的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集公共自行车站点·公共自行车站点智能采集系统·采集公共自行车站点导出csv
喵手26 分钟前
Python爬虫实战:地图 POI + 行政区反查实战 - 商圈热力数据准备完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·地区poi·行政区反查·商圈热力数据采集
熊猫_豆豆31 分钟前
YOLOP车道检测
人工智能·python·算法
nimadan1232 分钟前
**热门短剧小说扫榜工具2025推荐,精准捕捉爆款趋势与流量
人工智能·python
默默前行的虫虫37 分钟前
MQTT.fx实际操作
python
YMWM_1 小时前
python3继承使用
开发语言·python
JMchen1231 小时前
AI编程与软件工程的学科融合:构建新一代智能驱动开发方法学
驱动开发·python·软件工程·ai编程
亓才孓1 小时前
[Class类的应用]反射的理解
开发语言·python
小镇敲码人2 小时前
深入剖析华为CANN框架下的Ops-CV仓库:从入门到实战指南
c++·python·华为·cann