java - Spring MVC 和 jackson 不支持内容类型 'application/json'

标签 java json spring spring-mvc

尝试使用 Spring MVC 接收发布请求时出现错误(处理程序执行导致异常:不支持内容类型“application/json”)。

我的 Json,只是为了测试,非常简单:

{ "test": "abc123" }

我的 pojo 类:

public class Request {

    String test;

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

还有我的 Controller :

@RequestMapping(value = "/testing", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
private void testing(@RequestBody Request body, @RequestHeader HttpHeaders headers, HttpServletRequest httpRequest) {
    System.out.println(body.getTest());
}

在我的 pom.xml 中,我添加了:

<dependencies>
    ...
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.4.3</version>
    </dependency>
</dependencies>

我认为json反序列化有问题,但我找不到。

欢迎任何帮助。谢谢。

最佳答案

在我的例子中,问题是添加了一个属性的 dto,该属性的类型对于 Jackson 来说很容易出错,它是 JsonObject 类型。由于该添加,Jackson 无法反序列化其他对象。
异常消息完全不准确!

关于java - Spring MVC 和 jackson 不支持内容类型 'application/json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377311/

相关文章:

java - 如何使用 java Spring MVC 安排邮件

java - 链表删除错误

java - 从RecyclerView迁移到PagedListAdapter,使用submitList不显示数据(Android Studio JAVA)

ios - 在 if/else 语句中查询 NSDictionary 的值

java - 无法使用修饰符访问 org.springframework.aop.TruePointcut 类的成员 public

java - 如何使用 Spring MVC RequestMapping 有多个参数?

java - 通过命令行在 Windows 机器上静默安装 JDK 8 和 JRE 8

php - 如何从php文件中获取jquery函数中的信息

java - 从 Json 到 Gson 的数据模型映射返回 null

java - spring中如何知道post请求体中是否提供了参数值?