英文:
How to specify request body in Spring boot for swagger UI?
问题
以下是用于Swagger的控制器注解示例:
@PostMapping("/getMediaDataProduct/V2")
@ResponseBody
@ApiOperation(value = "获取媒体数据产品 V2")
@ApiResponses({@ApiResponse(code = 200, message = "成功", response = MediaDataProductResponseV2.class)})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "xxxxx", value = "xxxx", paramType = "header", required = true),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header"),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
// @ApiImplicitParam(name = "xxxxxx", value = "xxx", paramType = "header", required = true),
@ApiImplicitParam(name = "xxxxxx", value = "xxxxx", paramType = "header"),
@ApiImplicitParam(name = "xxxxxxx", value = "xxxxxxxx", paramType = "header", required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);
MediaDataProductResponseV2 response = mediaDataService.getMediaDataProductV2(request);
HttpStatus status = getHttpStatus(response.getStatusMessages(), response.getSystemErrors());
List<StatusMessage> statusMessages = appendSuccessStatusMessage(response.getStatusMessages(), status);
if(statusMessages !=null) {
response.setStatusMessages(statusMessages);
}
return new ResponseEntity<>(response, new HttpHeaders(), status);
}
你如何指定请求体以便在Swagger UI中显示?
更新:我在许多论坛上进行了研究,但似乎没有找到答案。 "如何通过使用Swagger注解指定请求体类,就像我在ApiResponses中所做的那样?"。 Swagger UI中创建了一个默认的用于请求体的参数,但不知道如何通过引用请求体类来指定它。
英文:
I have the following annotated controller for swagger:
@PostMapping("/getMediaDataProduct/V2")
@ResponseBody
@ApiOperation(value = "get media data product v2")
@ApiResponses({@ApiResponse(code = 200, message = "Successful", response = MediaDataProductResponseV2.class)})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "xxxxx", value = "xxxx", paramType = "header", required = true),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header"),
@ApiImplicitParam(name = "xxxx", value = "xxxx", paramType = "header", required = true),
// @ApiImplicitParam(name = "xxxxxx", value = "xxx", paramType = "header" ,required = true),
@ApiImplicitParam(name = "xxxxxx", value = "xxxxx", paramType = "header"),
@ApiImplicitParam(name = "xxxxxxx", value = "xxxxxxxx", paramType = "header", required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);
MediaDataProductResponseV2 response = mediaDataService.getMediaDataProductV2(request);
HttpStatus status = getHttpStatus(response.getStatusMessages(), response.getSystemErrors());
List<StatusMessage> statusMessages = appendSuccessStatusMessage(response.getStatusMessages(), status);
if(statusMessages !=null) {
response.setStatusMessages(statusMessages);
}
return new ResponseEntity<>(response, new HttpHeaders(), status);
}
How can you specify the request body so it's displayed in the swagger UI?
update: I have tried to research on this in many forums but doesn't seem to find an answer. "How can you specify the request body class by using swagger annotations just as I do with ApiResponses?". There is a default parameter in swagger UI created for body but don't know how to specify it by referencing the request body class.
答案1
得分: 1
如果我正确理解您的问题,您需要在Swagger定义中显示响应类型。大多数情况下,Swagger会自动将您的方法返回类型映射为响应类型。
但是,当它是一个泛型类时(就像在您的情况下是ResponseEntity
),它就无法做到这一点。
如果您可以将返回类型设置为类似于ResponseEntity<MediaDataProductResponseV2>
,那么应该可以正常工作。
英文:
If I understood your problem correctly, you need to have a response type shown in the swagger definition. Most of the time Swagger will automatically map your method return type as response type.
But, it fails to do so when it's a generic class, as in your case its ResponseEntity
.
If you can make your return type to something like ResponseEntity<MediaDataProductResponseV2>
, it should work.
答案2
得分: 0
根据Swagger文档,您只能在@ApiImplicitParam中使用以下值来声明paramType属性:
有效值包括path、query、body、header或form。
默认值为“”。
在您的情况下,尝试像这样做:
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "request", value = "The body is a simple string", paramType = "body", required = true),
@ApiImplicitParam(name = "headers", value = "It'll contain all header attributes in request", paramType = "header", required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);
}
英文:
According to Swagger documentation, you can declare paramType attribute at @ApiImplicitParam only with the following values:
> Valid values are path, query, body, header or form.
> the Default is ""
In your case, try to do something like this.
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "request", value = "The body is a simple string", paramType = "body", required = true),
@ApiImplicitParam(name = "headers", value = "It'll contain all header attributes in request", paramType = "header", required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug("/getMediaDataProduct/V2: this.mediaDataService: " + this.mediaDataService);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论