如何在Spring Boot中为Swagger UI指定请求体?

huangapple go评论67阅读模式
英文:

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(&quot;/getMediaDataProduct/V2&quot;)
@ResponseBody
@ApiOperation(value = &quot;get media data product v2&quot;)
@ApiResponses({@ApiResponse(code = 200, message = &quot;Successful&quot;, response = MediaDataProductResponseV2.class)})
@ApiImplicitParams(value = {
@ApiImplicitParam(name = &quot;xxxxx&quot;, value = &quot;xxxx&quot;, paramType = &quot;header&quot;, required = true),
@ApiImplicitParam(name = &quot;xxxx&quot;, value = &quot;xxxx&quot;, paramType = &quot;header&quot;, required = true),
@ApiImplicitParam(name = &quot;xxxx&quot;, value = &quot;xxxx&quot;, paramType = &quot;header&quot;),
@ApiImplicitParam(name = &quot;xxxx&quot;, value = &quot;xxxx&quot;, paramType = &quot;header&quot;, required = true),
//  @ApiImplicitParam(name = &quot;xxxxxx&quot;, value = &quot;xxx&quot;, paramType = &quot;header&quot; ,required = true),
@ApiImplicitParam(name = &quot;xxxxxx&quot;, value = &quot;xxxxx&quot;, paramType = &quot;header&quot;),
@ApiImplicitParam(name = &quot;xxxxxxx&quot;, value = &quot;xxxxxxxx&quot;, paramType = &quot;header&quot;, required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug(&quot;/getMediaDataProduct/V2: this.mediaDataService: &quot; + this.mediaDataService);
MediaDataProductResponseV2 response = mediaDataService.getMediaDataProductV2(request);
HttpStatus status = getHttpStatus(response.getStatusMessages(), response.getSystemErrors());
List&lt;StatusMessage&gt; statusMessages = appendSuccessStatusMessage(response.getStatusMessages(), status);
if(statusMessages !=null) {
response.setStatusMessages(statusMessages);
}
return new ResponseEntity&lt;&gt;(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&lt;MediaDataProductResponseV2&gt;,那么应该可以正常工作。

英文:

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&lt;MediaDataProductResponseV2&gt;, 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 = &quot;request&quot;, value = &quot;The body is a simple string&quot;, paramType = &quot;body&quot;, required = true),
@ApiImplicitParam(name = &quot;headers&quot;, value = &quot;It&#39;ll contain all header attributes in request&quot;, paramType = &quot;header&quot;, required = true)})
public ResponseEntity getMediaDataProductV2(@RequestBody final String request, @RequestHeader final HttpHeaders headers) {
Slogger.get().debug(&quot;/getMediaDataProduct/V2: this.mediaDataService: &quot; + this.mediaDataService);

huangapple
  • 本文由 发表于 2020年8月22日 01:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定