已解決: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序列化在整個應用中的行為
???【其他錯誤】
如果出現模塊錯誤
進入控制臺輸入:建議使用國內鏡像源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/