gpt4 book ai didi

java - Spring中的POST方法不接受application/json

转载 作者:行者123 更新时间:2023-12-02 01:26:22 25 4
gpt4 key购买 nike

在 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
+ "]";
}


}

postman 请求:

enter image description here

目的是Post方法只接收Email的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>

最佳答案

您应该在类顶部定义@RequestMapping("/api/call")以启用您的路由。 @RestController("/api/call") @RestController 中的值定义将由 Spring 管理的组件名称。

关于java - Spring中的POST方法不接受application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56858376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com