Spring boot Spring控制器获取bodyRequest null
我在试图通过邮递员处理POST请求时遇到问题。 在我的控制器中,我有:Spring boot Spring控制器获取bodyRequest null,spring-boot,controller,Spring Boot,Controller,我在试图通过邮递员处理POST请求时遇到问题。 在我的控制器中,我有: @ApiOperation(value = "xxxx", notes = "xxxx", response = String.class, authorizations = { @Authorization(value = "basicAuth") }, tags={ "saveCourse", }) @ApiResponses(value = { @ApiResponse(code = 200
@ApiOperation(value = "xxxx", notes = "xxxx", response =
String.class, authorizations = {
@Authorization(value = "basicAuth")
}, tags={ "saveCourse", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation", response =
String.class),
@ApiResponse(code = 404, message = "Not found", response =
String.class),
@ApiResponse(code = 405, message = "Invalid input", response =
String.class),
@ApiResponse(code = 500, message = "Internal Server Error", response =
String.class),
@ApiResponse(code = 200, message = "unexpected error", response =
String.class) })
@RequestMapping(value = "/course/saveCourse",
produces = { "application/json"},
consumes = { "application/json"},
method = RequestMethod.POST)
ResponseEntity<String> saveCourse(@ApiParam(value = "xxxxx" ,required=true ) @RequestBody Course coure){
LOG.info(course.toString);
}
班级学生:
public class Student implements Serializable {
@JsonProperty("studentId")
private String studentId = null;
@JsonProperty("studentName")
private String studentName = null;
@JsonProperty("studAge")
private int studAge = null;
// getters & setters
// ...
}
在“邮递员”中,我发送一个带有标题的POST请求:
Content-Type : application/json
该机构:
{
"prof": {
"profLastName":"test",
"profFirstName":"test",
"age":"30"
},
"students" :[
"{'studentId':'0','studentName':'','studAge':'00'}",
"{'studentId':'2','studentName':'','studAge':'21'}",
"{'studentId':'4','studentName':'','studAge':'40'}",
"{'studentId':'6','studentName':'','studAge':'60'}"
]
}
当我处理请求时,我得到的RequestBody为空:
[http-nio-xxxx-exec-4]INFO com.test.myControllerIml-课堂课程{
教授:空
学生:[]
}
你的要求是错误的
你应该使用
{
"prof": {
"profLastName":"test",
"profFirstName":"test",
"age":"30"
},
"students" :[
{"studentId":"0","studentName":"","studAge":"00"},
{"studentId":"2","studentName":"","studAge":"21"},
{"studentId":"4","studentName":"","studAge":"40"},
{"studentId":"6","studentName":"","studAge":"60"}
]
}
你的要求是错误的
你应该使用
{
"prof": {
"profLastName":"test",
"profFirstName":"test",
"age":"30"
},
"students" :[
{"studentId":"0","studentName":"","studAge":"00"},
{"studentId":"2","studentName":"","studAge":"21"},
{"studentId":"4","studentName":"","studAge":"40"},
{"studentId":"6","studentName":"","studAge":"60"}
]
}
你好,梅拉德,我尝试了你的解决方案,但结果相同。谢谢你,你的代码对我来说很好。我注意到一些编译错误,并在本地修复了它。你好,Melad,我尝试了你的解决方案,但得到了相同的结果。谢谢你,你的代码对我来说很好。我注意到一些编译错误,并在本地修复了它。当我调试我的请求:CharStreams.toString(request.getReader())时,我得到了我发送的json,所以问题可能在映射中。注意:我们使用Swagger生成API。当我调试我的请求时:CharStreams.toString(request.getReader())我得到了我发送的json,所以问题可能在映射中。注:我们使用Swagger来生成API。
{
"prof": {
"profLastName":"test",
"profFirstName":"test",
"age":"30"
},
"students" :[
{"studentId":"0","studentName":"","studAge":"00"},
{"studentId":"2","studentName":"","studAge":"21"},
{"studentId":"4","studentName":"","studAge":"40"},
{"studentId":"6","studentName":"","studAge":"60"}
]
}