Reputation: 615
In a Spring api, I have a method in a RestController for POST requests, however, when the request is made by Postman the system returns error 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 class:
@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();
}
}
Email class:
public class Email {
private static String MAIL_TO = "[email protected]";
@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
+ "]";
}
}
Postman request:
The purpose is that the Post method only receives a Json for Email and returns success after sending the email, but it returns error 415, I tried @PostMapping(consumes ="application/json", produces="application/json")
but it did not work. What is left for the system to accept Json requests?
Edited
My 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>
Upvotes: 0
Views: 1342
Reputation: 481
You should define the @RequestMapping("/api/call")
on top of class to enable your route. @RestController("/api/call")
that value inside @RestController
defines the component name that will be managed by the Spring.
Upvotes: 2