我正在使用Spring Boot和Web依赖项创建一个简单的rest控制器。我正在尝试将JSON正文反序列化为仅具有3个字段的测试POJO,但是当我尝试发出POST请求时,服务器将响应500错误,并且我在控制台中看到的错误是:
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap
我编写的所有代码如下:
EmailApplication.java:
package com.test.email.app;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = { "com.test.email" })
public class EmailApplication {
public static void main(String[] args) {
SpringApplication.run(EmailApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("----- You're up and running with test-email-app! -----");
};
}
}
EmailController.java:
package com.test.email.controller;
import com.test.email.entity.TestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import java.net.URI;
@RestController
public class EmailController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
TestEntity entity = new TestEntity();
return "test-email-app index";
}
@RequestMapping(value = "/poster", method = RequestMethod.POST)
public ResponseEntity<String> poster(@RequestBody TestEntity testEntity) {
URI location = URI.create("/poster");
return ResponseEntity.created(location).body(testEntity.getUrl());
}
}
TestEntity.java
package com.test.email.entity;
/**
* An entity for serialization/deserialization testing
*/
public class TestEntity {
public String url;
public int count;
public double height;
public TestEntity() {}
public TestEntity(String url, int count, double height) {
this.url = url;
this.count = count;
this.height = height;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
我仅使用标题Content-Type: application/json
和正文向Postman发出POST请求:
{ "url": "http://google.com", "count": 3, "height": 2.4 }
我不知道为什么Spring无法将LinkedHashMap转换为我的POJO。任何帮助将不胜感激。
编辑:
我的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.email</groupId>
<artifactId>test-email-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-email-app</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:4)
分析您的问题后,我发现某些情况下可能会出现此问题。 那些在下面:
如果您的班级TestEntity
是端点的内部班级。
只需将您的TestEntity
从内部课程转移到一个单独的课程,然后运行您的代码,即可使用。
如果您想支持内部类RequestBody,然后将TestEntity
类设置为静态,这将解决您的问题。
如果仍然不能解决问题,请检查您对pom.xml
的依赖性。可能是您没有添加对正确方法的依赖。为此,您可以查看this answer。或者,您可以在jackson
文件中明确添加pom.xml
。
依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
希望这会解决您的问题:)
如果仍然不能解决问题,请查看一些youtube视频或博客网站,或从github下载一些开源项目。
谢谢:)
答案 1 :(得分:2)
尝试指定您的方法使用JSON。更改注释:
@RequestMapping(value = "/poster", method = RequestMethod.POST)
到
@PostMapping(value = "/poster",
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.TEXT_HTML)
您在此处指定的主要部分是希望输入的内容是JSON。不确定返回的类型,但是您可以指定适当的类型或删除“生产”部分。主要部分是指定输入内容
答案 2 :(得分:0)
理想情况下,此代码应该可以工作,但是可以通过在pom.xml中添加以下依赖项来进行尝试
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>