【问题标题】:Spring controller getting bodyRequest nullSpring控制器获取bodyRequest null
【发布时间】: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。

标签: spring-boot controller


【解决方案1】:

你请求的正文是错误的 你应该使用

{
    "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,我尝试了您的解决方案,但结果相同。谢谢
  • 您的代码对我来说很好用。我注意到一些编译错误并在本地修复。
猜你喜欢
  • 1970-01-01
  • 2021-04-21
  • 2016-07-20
  • 2018-10-04
  • 2022-01-08
  • 2013-05-08
  • 2021-06-09
  • 2017-12-10
相关资源
最近更新 更多