在Spring api中,我在RestController中有一个用于POST请求的方法,但是,当Postman发出请求时,系统返回错误415:
{
"timestamp": "2019-07-02T18:08:48.859+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",
"path": "/api/call"
}
RestController类:
@RestController("/api/call")
public class CallController {
private final MailService mailService;
public CallController(MailService mailService) {
super();
this.mailService = mailService;
}
@PostMapping
public ResponseEntity<Void> postCall(@Valid @RequestBody Email formEmail) throws URISyntaxException {
mailService.sendMail();
return ResponseEntity.noContent().build();
}
}
电子邮件类别:
public class Email {
private static String MAIL_TO = "mail@email.com";
@Null
private String mailTo;
@NotNull
@Size(min = 3)
private String nameUserRequest;
@NotNull
@javax.validation.constraints.Email
private String emailUserRequest;
@NotNull
private String text;
public Email() {
super();
}
public String getMailTo() {
mailTo = MAIL_TO;
return mailTo;
}
public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}
public String getNameUserRequest() {
return nameUserRequest;
}
public void setNameUserRequest(String nameUserRequest) {
this.nameUserRequest = nameUserRequest;
}
public String getEmailUserRequest() {
return emailUserRequest;
}
public void setEmailUserRequest(String emailUserRequest) {
this.emailUserRequest = emailUserRequest;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "Email [nameUserRequest=" + nameUserRequest + ", emailUserRequest=" + emailUserRequest + ", text=" + text
+ "]";
}
}
邮递员请求:
目的是Post方法仅接收电子邮件的Json并在发送电子邮件后返回成功,但是它返回错误415,我尝试了@PostMapping(consumes ="application/json", produces="application/json")
,但是没有用。系统还剩下什么来接受Json请求?
已编辑
我的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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company.mail.call</groupId>
<artifactId>mail-call</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mail-call</name>
<description>Mail call</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</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>
答案 0 :(得分:2)
您应在课程顶部定义@RequestMapping("/api/call")
以启用您的路线。 @RestController("/api/call")
@RestController
中的值定义了将由Spring管理的组件名称。