如图所示,添加操作显示为“响应类(状态 200)”。但是,添加操作已以永远不会返回 200 的方式实现。成功时返回 201。
我的问题是如何将(状态 200)更改为(状态 201)?这部分的代码如下:
@RequestMapping(method = RequestMethod.PUT, value = "/add")
@ApiOperation(value = "Creates a new person", code = 201)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Record created successfully"),
@ApiResponse(code = 409, message = "ID already taken")
})
public ResponseEntity<String> add(@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "id", required = true) String id) {
if (PD.searchByID(id).size() == 0) {
Person p = new Person(name, id);
PD.addPerson(p);
System.out.println("Person added.");
return new ResponseEntity<String>(HttpStatus.CREATED);
} else {
System.out.println("ID already taken.");
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
谢谢!