【发布时间】:2017-07-04 15:58:21
【问题描述】:
我在尝试通过 POSTMAN 处理 POST 请求时遇到问题。 在我的控制器中,我有:
@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 Course implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@JsonProperty("prof")
private Prof prof = null;
@JsonProperty("students")
private List<Strudent> students = new ArrayList<Strudent>();
// getters & setters
// ...
}
教授班:
public class Prof implements Serializable {
@JsonProperty("profLastName")
private String profLastName = null;
@JsonProperty("profFirstName")
private String profFirstName = null;
@JsonProperty("age")
private int age = null;
// getters & setters
}
班级学生:
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
// ...
}
在 POSTMAN 中,我正在发送带有标头的 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 null :
[http-nio-xxxx-exec-4] 信息 com.test.myControllerIml - 课程 { 教授:空 学生: [] }
【问题讨论】:
-
当我调试我的请求时:CharStreams.toString(request.getReader()) 我收到了我发送的 json,所以问题可能出在映射中。注意:我们使用 Swagger 来生成我们的 API。