问题描述
是否可以进行验证以避免在请求上发送任何额外的参数?
例如,在我的端点中,使用者应发送以下参数:name
,family
,email
我想通过发送诸如name
,family
,email
, phone
之类的多余内容来避免消费者。
我正在使用以下方式处理端点:
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);
}
解决方法
我认为您的问题没有解决方案。我认为,如果每个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'