如何修复 'HTTP-415' 错误,在使用 spring 启动的 REST Web 服务中的 POST 请求期间
How to fix 'HTTP-415' error, during POST request in REST web service using spring boot
我是 Spring 引导和学习的初学者。
如何修复 'HTTP-415' 错误,在 REST Web 服务中使用 Spring 启动的 POST 请求期间,如下所示?我试过 @RequestMapping
注释,@RequestParam
。 @RequestParam
给出了一些其他错误401。但是,415与@RequestMapping
和@PostMapping
一致。
@PostMapping
请求出现问题。
{
"timestamp": "2018-12-31T18:29:36.727+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type
'text/plain;charset=UTF-8' not supported\r\n\tat
org.springframework.web.servlet.mvc.method.annotation.
AbstractMessageConverterMethodArgumentResolver.
readWithMessageConverters
(AbstractMessageConverterMethodArgumentResolver.java:224)\r\n\tat
org.springframework.web.servlet.mvc.method.annotation.
RequestResponseBodyMethodProcessor.
readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)
\r\n\tat org.springframework.web.servlet.mvc.method.
annotation.RequestResponseBodyMethodProcessor.
resolveArgument(RequestResponseBodyMethodProcessor.java:130)
\r\n\tat...................
发出以下请求时:
StudentController.java
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
:
:
@PostMapping("/students/{studentId}/courses")
public ResponseEntity<Void> registerStudentForCourse(
@PathVariable String studentId,
@RequestBody Course newCourse) {
Course course = studentService.addCourse(studentId, newCourse);
if (course == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().
path("/{id}").buildAndExpand(course.getId()).toUri();
return ResponseEntity.created(location).build();
}
StudentService.java
@Component
public class StudentService {
:
:
public Course addCourse(String studentId, Course course) {
Student student = retrieveStudent(studentId);
if (student == null) {
return null;
}
String randomId = new BigInteger(130, random).toString(32);
course.setId(randomId);
student.getCourses().put(course.getId(), course);
return course;
}
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot</groupId>
<artifactId>student-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-services</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
很明显,在邮递员中,您正在以 form-urlencoded 的形式发送信息,但在您的控制器中,您期待一个请求 body(例如 json),因此您需要更改 @RequestBody
至 @ModleAtrributes
或使用 header Content-type: application/json
向您发送信息
问题已通过将内容 header 添加为 application/json
得到解决
您的控制器无需更改。将消费和生产添加到 FeignClient
的 @RequestMapping
。即使没有 @Headers
:
也能成功
@FeignClient(name = "myFeignClient", url = "${com.example.url}", configuration = FeignConfiguration.class)
@RequestMapping(value = "feign-my", consumes = "application/json", produces = "application/json")
//@Headers({ACCEPT + ": " + APPLICATION_JSON_VALUE, CONTENT_TYPE + ": " + APPLICATION_JSON_VALUE})
public interface MyFeignClient extends MyController {}
我是 Spring 引导和学习的初学者。
如何修复 'HTTP-415' 错误,在 REST Web 服务中使用 Spring 启动的 POST 请求期间,如下所示?我试过 @RequestMapping
注释,@RequestParam
。 @RequestParam
给出了一些其他错误401。但是,415与@RequestMapping
和@PostMapping
一致。
@PostMapping
请求出现问题。
{
"timestamp": "2018-12-31T18:29:36.727+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type
'text/plain;charset=UTF-8' not supported\r\n\tat
org.springframework.web.servlet.mvc.method.annotation.
AbstractMessageConverterMethodArgumentResolver.
readWithMessageConverters
(AbstractMessageConverterMethodArgumentResolver.java:224)\r\n\tat
org.springframework.web.servlet.mvc.method.annotation.
RequestResponseBodyMethodProcessor.
readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)
\r\n\tat org.springframework.web.servlet.mvc.method.
annotation.RequestResponseBodyMethodProcessor.
resolveArgument(RequestResponseBodyMethodProcessor.java:130)
\r\n\tat...................
发出以下请求时:
StudentController.java
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
:
:
@PostMapping("/students/{studentId}/courses")
public ResponseEntity<Void> registerStudentForCourse(
@PathVariable String studentId,
@RequestBody Course newCourse) {
Course course = studentService.addCourse(studentId, newCourse);
if (course == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().
path("/{id}").buildAndExpand(course.getId()).toUri();
return ResponseEntity.created(location).build();
}
StudentService.java
@Component
public class StudentService {
:
:
public Course addCourse(String studentId, Course course) {
Student student = retrieveStudent(studentId);
if (student == null) {
return null;
}
String randomId = new BigInteger(130, random).toString(32);
course.setId(randomId);
student.getCourses().put(course.getId(), course);
return course;
}
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot</groupId>
<artifactId>student-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-services</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
很明显,在邮递员中,您正在以 form-urlencoded 的形式发送信息,但在您的控制器中,您期待一个请求 body(例如 json),因此您需要更改 @RequestBody
至 @ModleAtrributes
或使用 header Content-type: application/json
问题已通过将内容 header 添加为 application/json
得到解决您的控制器无需更改。将消费和生产添加到 FeignClient
的 @RequestMapping
。即使没有 @Headers
:
@FeignClient(name = "myFeignClient", url = "${com.example.url}", configuration = FeignConfiguration.class)
@RequestMapping(value = "feign-my", consumes = "application/json", produces = "application/json")
//@Headers({ACCEPT + ": " + APPLICATION_JSON_VALUE, CONTENT_TYPE + ": " + APPLICATION_JSON_VALUE})
public interface MyFeignClient extends MyController {}