【问题标题】:How to annotate to require individual elements of requestBody in springdoc?如何在springdoc中注释要求requestBody的各个元素?
【发布时间】:2021-10-29 20:07:57
【问题描述】:

我有一个 spring(启动)服务器,并希望使用 springdoc 从注释中生成 openapi 规范。

我在请求正文中有一个带有两个参数的请求。我希望第一个是必需的,第二个是可选的

@RequestBody(required = {true|false}) 似乎只将正文中的所有参数设置为(不是)必需的。 另一方面,@Parameter 的 Javadoc 说要使用 io.swagger.v3.oas.annotations.parameters.RequestBody

这是我的代码,我希望生成一个规范,其中第一个参数是必需的,第二个是可选的:

    @GetMapping("/fstVector")
    public ResponseEntity<Vector> fstV(@RequestBody final Vector v1, @RequestBody(required = false) final Vector v2) {
        return new ResponseEntity<>(v1, HttpStatus.OK);
    }
    
    @PostMapping("/fstVector")
    public ResponseEntity<Vector> fstVPost(@RequestBody(required = true) final Vector v1, @RequestBody(required = false) final Vector v2) {
        return new ResponseEntity<>(v1, HttpStatus.OK);
    }

生成的规范需要两个参数:

  /pond/fstVector:
    get:
      tags:
      - circle-escape-controller
      operationId: fstV
      parameters:
      - name: v1
        in: query
        required: true
        schema:
          $ref: '#/components/schemas/Vector'
      - name: v2
        in: query
        required: true
        schema:
          $ref: '#/components/schemas/Vector'
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Vector'
    post:
      tags:
      - circle-escape-controller
      operationId: fstVPost
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                v1:
                  $ref: '#/components/schemas/Vector'
                v2:
                  $ref: '#/components/schemas/Vector'
        required: true
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Vector'

我怎样才能只要求所有四种请求类型的特定参数?

【问题讨论】:

  • 理想情况下,GET 请求不应采用请求正文(除非您真的想发送大对象),并且 POST 请求不应返回 OK
  • 什么是“所有四种请求类型”?
  • @ognjenkl 获取、放置、发布、删除
  • @peer DELETE 如何适应“第一个是必需的,第二个是可选的”?

标签: spring-boot openapi springdoc springdoc-openapi-ui


【解决方案1】:

重要

  • 到给定端点的请求正文不应超过 1 个!
  • 请求正文主要是一个 JSON 对象。因此,要强制要求正文中的某些属性,建议使用验证 API。
  • 有 2 个@RequestBody 注释。一个来自 Spring 框架 org.springframework.web.bind.annotation.RequestBody,另一个来自 io.swagger.v3.oas.annotations.parameters.RequestBody

重要的是,即使您使用 Swagger 库中的 io.swagger.v3.oas.annotations.parameters.RequestBody,您仍然需要使用 org.springframework.web.bind.annotation.RequestBody 来接收实际对象。

重构如下代码应该对您的情况有所帮助

控制器类

@GetMapping("/fstVector")
public ResponseEntity<Vector> fstV(
    // we generally use @RequestParam for query parameters. Query parameters are generally optional and thus the "required" attribute of @Parameter defaults to "false"
    @Parameter @RequestParam final Vector v1, 
    // set @Parameter to TRUE if the parameter must be passed.
    @Parameter(required = true) @RequestParam final Vector v2 
) {
    return new ResponseEntity<>(v1, HttpStatus.OK);
}
    
@PostMapping("/fstVector")
public ResponseEntity<Vector> fstVPost(
    // RequestBody objects are "required" by default. To make them optional, add "(required = false)"
    @org.springframework.web.bind.annotation.RequestBody   // Spring
    @io.swagger.v3.oas.annotations.parameters.RequestBody  // Swagger
    @Valid // Bean validation to ensure if the incoming object is valid
    final Vector v1
) {
    return new ResponseEntity<>(v1, HttpStatus.OK);
}

对于域对象,重构 DTO 如下

DTO

@Schema(description = "My DTO")
class Vector {
   // The below attribute is required
   @NotNull
   @Parameter(description = "my first attribute", required = true)
   String attribute1;

   // The below attribute is optional
   @Parameter(description = "my second attribute", required  = false)
   String attribute2;
}

【讨论】:

    【解决方案2】:

    不建议将请求正文添加到GET请求中,即使您可以...看here

    控制器类方法:

    @GetMapping("/fstVector")
    public ResponseEntity<Vector> fstV(
            @RequestParam final Vector v1,
            @RequestParam(required = false) final Vector v2) {
        return new ResponseEntity<>(v1, HttpStatus.OK);
    }
    
    @PostMapping("/fstVector")
    public ResponseEntity<Vector> fstVPost(
            @Valid
            @org.springframework.web.bind.annotation.RequestBody
            @io.swagger.v3.oas.annotations.parameters.RequestBody
            final VectorRequest vectorRequest) {
        return new ResponseEntity<>(vectorRequest.getV1(), HttpStatus.OK);
    }
    

    DTO 对象:

    public class VectorRequest {
        @NotNull
        Vector v1;
        Vector v2;
    
        public Vector getV1() {
            return v1;
        }
    
        public void setV1(Vector v1) {
            this.v1 = v1;
        }
    
        public Vector getV2() {
            return v2;
        }
    
        public void setV2(Vector v2) {
            this.v2 = v2;
        }
    }
    

    参数v1是必需的,v2是可选的。

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多