【问题标题】:How to avoid extra inputs request in OpenApi & Java?如何避免 OpenApi & Java 中的额外输入请求?
【发布时间】:2020-10-07 09:34:11
【问题描述】:

是否可以进行验证以避免在请求中发送任何额外参数?

例如,在我的端点中,消费者应该发送这些参数:namefamilyemail

我想通过发送任何额外信息来避开消费者,例如:namefamilyemailphone

我正在使用以下方式来处理端点:

OpenApi Swagger 规范:

post:
  tags:
    - '/user'
  operationId: saveUser
  requestBody:
    required: true
    content:
      application/x-www-form-urlencoded:
        schema:
          type: object
             properties:
                name:
                   type: string
                family:
                   type: string
                email:
                   type: string

Java:

@RequestMapping(value = "/user", consumes = { "application/x-www-form-urlencoded" }, method = RequestMethod.POST)
default ResponseEntity<Void> postUser(
    @ApiParam(hidden = true) @RequestParam(value = "name", required = true) String name,
    @ApiParam(hidden = true) @RequestParam(value = "family", required = true) String family,
    @ApiParam(hidden = true) @RequestParam(value = "email", required = true) String email
) {
    return getDelegate().saveUser(name, family, email);
}

【问题讨论】:

  • 您好,我不确定我是否理解问题,是消费者发送更多数据字段吗?这是一个问题吗?
  • 是的,出于某种原因,我需要强制消费者只发送我在端点中定义的内容。 @MartinByers

标签: java spring api swagger openapi


【解决方案1】:

我认为您的问题没有解决方案。我认为,如果每个 API 开发人员能够限制用户发送的错误内容,他会更加高兴。 您可以做的是检查您的 java 方法中的内容并返回相应的 HTTP 代码以指示错误。您可以像这样直接在 YAML OpenAPI 中添加状态码:

  responses:
    "200":
      description: Correct Request, all Parameters are correct
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/RequestSuccessful'
    "400":
      description: Bad input, some parameters are wrong or missing
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/RequestError'

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 2012-12-18
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多