简体   繁体   English

使用 HttpServletRequest 的 Swagger 文档

[英]Using Swagger Documentation for HttpServletRequest

I am new to swagger and using it's documentation.我是 swagger 的新手并使用它的文档。 I am currently trying to use swagger to display the request body of a PATCH request.我目前正在尝试使用 swagger 来显示 PATCH 请求的请求正文。 Previously, the parameter of the PATCH method was the DTO of the object that was being updated, which made it very easy to display the attributes of the object (as I am using SpringBoot, and using @Schema worked perfectly).以前,PATCH方法的参数是正在更新的object的DTO,这样可以很容易地显示object的属性(因为我使用的是SpringBoot,并且使用@Schema效果很好)。 However, now the parameter of the PATCH method is an HttpServletRequest.但是,现在 PATCH 方法的参数是一个 HttpServletRequest。 Instead of displaying the HttpServletRequest in the swagger doc (which seems to automatically be happening), I want to show the DTOs attributes (just as had been done before).我不想在 swagger 文档中显示 HttpServletRequest(这似乎是自动发生的),而是想显示 DTO 属性(就像之前所做的那样)。 I was wondering if there was a way to do that?我想知道是否有办法做到这一点?

Any advice is much appreciated!非常感谢任何建议!

I am assuming you are using springdoc-openapi for generating SwaggerUI.我假设您正在使用 springdoc-openapi 来生成 SwaggerUI。

To use this you can use the below Maven dependencies,要使用它,您可以使用以下 Maven 依赖项,

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-ui</artifactId>
  <version>1.4.2</version>
</dependency>
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-webmvc-core</artifactId>
  <version>1.4.2</version>
</dependency>

From v1.1.25 of springdoc-openapi, HttpServletRequest and HttpServletResponse will be added to the list of ignored types.从 springdoc-openapi v1.1.25 开始,HttpServletRequest 和 HttpServletResponse 将被添加到忽略类型列表中。

See below,见下文,

https://github.com/springdoc/springdoc-openapi/issues/57 https://github.com/springdoc/springdoc-openapi/issues/57

So even if we add HttpServletRequest as a parameter inside the controller method, it will be ignored and will not be displayed in the swagger.因此即使我们在 controller 方法中添加 HttpServletRequest 作为参数,它也会被忽略,不会显示在 swagger 中。

So coming back to your question, to display the model of a class, you can describe another parameter along with HttpServletRequest as below,所以回到你的问题,要显示 class 的 model,你可以描述另一个参数以及 HttpServletRequest 如下,

@Operation(summary = "Returns a token", description = "Returns A token API", tags = "tokenGeneration", responses = {

            @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Timeresponse.class))),
            @ApiResponse(description = "not found Operation", responseCode = "404") })
    @PatchMapping("/getTokenPatchRequest")
    public ResponseEntity getTokenpatch(HttpServletRequest request, @RequestBody AuthReq2 req) {

        log.info("The HttpServlet request header contains the information : " + request.getHeader("Authorization"));

The model class of Auth2 is as below which can describe your example value of username and passwords etc. Auth2 的 model class 如下,可以描述您的用户名和密码等示例值。

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class AuthReq2 {
    
    @Schema(example = "diannamcallister")
    private String userName;
    
    @Schema(example = "test")
    private String password;

}

Ands finally the swagger page looks like this,最后 swagger 页面看起来像这样,

在此处输入图像描述

When you enter something in the authorization header, as below,当您在授权 header 中输入内容时,如下所示,

在此处输入图像描述

This can be accessed via the HTTP servlet request via the below code,这可以通过以下代码通过 HTTP servlet 请求访问,

log.info("The HttpServlet request header contains the information : " + request.getHeader("Authorization"));

The log entry in the springboot application will look like below, springboot 应用程序中的日志条目如下所示,

10:40:01.876 INFO   OpenApiController.getTokenpatch:163 - The HttpServlet request header contains the information : stackoverflow

The above answer did not work since adding another parameter to the method broke the functionality of the method itself.上面的答案不起作用,因为向方法添加另一个参数会破坏方法本身的功能。

The solution that worked was in the controller to add the content parameter to the @RequestBody annotation:有效的解决方案是在 controller 中将 content 参数添加到@RequestBody注释中:

@RequestBody(description = "Description.", 
           content = @Content(schema = @Schema(implementation = ObjectDTO.class)));

How to displaying the HttpServletRequest in the swagger doc?如何在 swagger 文档中显示 HttpServletRequest?

You can set in the configuration of swagger2, SwaggerConfig.java可以在swagger2的配置中设置, SwaggerConfig.java

new Docket(DocumentationType.SWAGGER_2)
                ...
                .ignoredParameterTypes(HttpSession.class, HttpServletRequest.class, HttpServletResponse.class)
                .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM