[英]How to change the response status code for successful operation in Swagger?
As shown in the image, it says "Response Class (Status 200)" for the add operation. 如图中所示,添加操作显示为“ Response Class(Status 200)”。 However, the add operation has been implemented in such a way that it will never return 200. It returns 201 on success.
但是,添加操作已实现为永远不会返回200。成功返回201。
My question is how can I change the (Status 200) to (Status 201)? 我的问题是如何将(状态200)更改为(状态201)? The code for this part is as follows:
该部分的代码如下:
@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);
}
}
Thanks! 谢谢!
You can add the @ResponseStatus
annotation to any a controller method to define the http status it should return. 您可以将
@ResponseStatus
批注添加到任何控制器方法中,以定义应返回的http状态。 Ex 防爆
Adding the following annotation on acontroller method: 在acontroller方法上添加以下注释:
@ResponseStatus(code = HttpStatus.CREATED)
Will return a HTTP status 201 (Created) 将返回HTTP状态201(已创建)
在控制器方法(方法= requestMethod.PUT)或(方法= requestMethod.POST)@ResponseStatus(代码= HttpStatus.ACCEPTED)中添加以下注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.