我创建了@RestController并映射了@PostMapping请求。
我将内容类型作为“application / json”传递给控制器,我正在阅读JSON,并将@RequestBody注释映射到我的Model类对象。
@RestController:
@PostMapping("/saveUser")
public UserDetails save(@RequestBody UserDetails user) {
System.out.println("from json input : "+user.idVal+" : "+user.getName());
service.save(user);
JSONObject jsn = new JSONObject();
jsn.put("Result", user.getName());
return user;
}
HTTP请求
{
"idVal":"asdadasd",
"name":"TestUser"
}
模型类
import javax.persistence.Entity;
javax.persistence.Id;
@Entity
public class UserDetails {
@Id
public String idVal;
public String name;
public String getId() {
return idVal;
}
public void setId(String id) {
this.idVal = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我使用Postman作为REST客户端。
以下是我得到的错误:
HTTP Status 415 – Unsupported Media Type
Type Status Report
Description The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.
Apache Tomcat/8.5.31
我尝试过如下:
@PostMapping(value="/saveUser", consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public UserDetails save(@RequestBody UserDetails user) {
System.out.println("from json input : "+user.idVal+" : "+user.getName());
service.save(user);
JSONObject jsn = new JSONObject();
jsn.put("Result", user.getName());
return user;
}
编辑:
以下是完整的邮递员申请:
POST /Zcrud/saveUser HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: fa4cfc76-d60c-f8dd-53b9-fd8b0b27ef7d
{
"idVal":"asdadasd",
"name":"TestUser"
}
响应:
<!doctype html><html lang="en"><head><title>HTTP Status 415 – Unsupported Media Type</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 415 – Unsupported Media Type</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.</p><hr class="line" /><h3>Apache Tomcat/8.5.31</h3></body></html>
答案 0 :(得分:0)
您的请求中的application/json
已从邮递员传递到您的应用。但是您的服务器无法理解application/json
是什么。
所以,你必须在你的服务器中定义,将处理什么类型的请求,以及它将产生什么类型的响应。
要执行此操作,请更改@RestController
@PostMapping
,如下所示
@PostMapping(value ="/saveUser", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public UserDetails save(@RequestBody UserDetails user) {
System.out.println("from json input : "+user.idVal+" : "+user.getName());
service.save(user);
JSONObject jsn = new JSONObject();
jsn.put("Result", user.getName());
return user;
}
希望它有所帮助!! :)
答案 1 :(得分:0)
将Content-Type:application / json; odata = verbose添加到@PostMapping注释中