Spring控制器获取bodyRequest null

时间:2017-07-04 15:58:22

标签: spring-boot controller

我在尝试通过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] INFO com.test.myControllerIml - class Course {       prof:null       学生们: []   }

1 个答案:

答案 0 :(得分:0)

你要求身体是错的 你应该使用

{
    "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"}
    ]
}