[英]400 Bad request trying to POST to a Spring endpoint
Trying to post this to my Spring api 试着把它发布到我的Spring api
{
"test1":"test1",
"test2":"test2",
"test3":"test3"
}
But i keep getting a 400 Bad Request
response. 但我一直收到
400 Bad Request
响应。
Controller looks like this: 控制器看起来像这样:
@RestController
@RequestMapping("/rest/api")
public class DokumentController {
@PostMapping(value = "/test", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity test(@RequestBody TestDTO body) {
System.out.println(body.getTest1());
return ResponseEntity.ok().build();
}
}
TestDTO looks like this: TestDTO看起来像这样:
public class TestDTO {
private String test1;
private String test2;
private String test3;
TestDTO(String test1, String test2, String test3) {
this.test1 = test1
this.test2 = test2
this.test3 = test3
}
public String getTest1() {
return test1;
}
public void setTest1(String test1) {
this.test1 = test1;
}
//And so on..
}
Can someone tell me what I'm doing wrong here? 谁能告诉我这里我做错了什么?
You have to create a constructor with no arguments in your DTO class: 您必须在DTO类中创建一个不带参数的构造函数:
public class TestDTO {
private String test1;
private String test2;
private String test3;
//Default no argument constructor
TestDTO() {
}
TestDTO(String test1, String test2, String test3) {
this.test1 = test1
this.test2 = test2
this.test3 = test3
}
public String getTest1() {
return test1;
}
public void setTest1(String test1) {
this.test1 = test1;
}
//And so on..
}
The Jackson Framework creates the class first and uses reflection to set the values. Jackson框架首先创建类,并使用反射来设置值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.