基礎
SpringBoot接受到http請求後,會根據content-type來判斷請求的數據格式,進而利用HttpMessageConverter將數據解析成特定的類型。對應JSON格式的數據,SpringBoot默認使用jackson進行數據的序列化/反序列化;
可能原因
- 原因1:缺少jackson相關的jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
對於SpringBoot, 添加spring-boot-starter-web後會自動引入這些依賴;
- 原因2:沒有解析JSON格式的Converter,比如默認的MappingJackson2HttpMessageConverter(利用jackson),或者FastJsonHttpMessageConverter(利用fastjson);
- 原因3:Converter反序列化解析JSON時失敗;
參考:
- https://www.iteye.com/blog/aoyouzi-2153707