【发布时间】:2019-04-05 17:52:34
【问题描述】:
当我使用Postman 发送带有 JSON 正文 (application/json) 的 post 请求时,我在 spring-mvc 中收到此错误(我没有使用 spring boot)
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
我尝试了有关此错误的所有 SO 主题,但没有任何效果:(
我还在pom.xml 中包含了Jackson 的依赖项,以将JSON 对象映射到POJO。
那么为什么它一直告诉我Content type 'application/json' not supported!
我的控制器
@RestController
public class FooRest {
// even with consumes=MediaType.APPLICATION_JSON_VALUE it does not work
@RequestMapping(value = "/api/foo", method = RequestMethod.POST)
public String foo(HttpServletRequest request, @RequestBody FooBean bean) {
...
}
}
我的配置
@Configuration
@EnableWebMvc
@ComponentScan({"controllers"})
public class AppConfig implements WebMvcConfigurer { }
我的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
...
</dependencies>
卷曲版
POST /MY-API HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 89859e26-813d-fb53-8726-57900f02207e
{
//JSON OBJECT
}
解决方案
我变了
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
到
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
终于成功了 :) 有人能解释一下为什么吗?
【问题讨论】:
标签: java spring spring-mvc jackson