Spring boot 如何在springdoc中注释以要求requestBody的各个元素?
我有一个spring(boot)服务器,希望使用springdoc从注释生成openapi规范 我有一个请求,请求正文中有两个参数。我希望第一个是必需的,第二个是可选的Spring boot 如何在springdoc中注释以要求requestBody的各个元素?,spring-boot,openapi,springdoc,springdoc-openapi-ui,Spring Boot,Openapi,Springdoc,Springdoc Openapi Ui,我有一个spring(boot)服务器,希望使用springdoc从注释生成openapi规范 我有一个请求,请求正文中有两个参数。我希望第一个是必需的,第二个是可选的 @RequestBody(required={true | false})似乎只将body中的所有参数设置为(not)required。 另一方面,@Parameter的Javadoc表示使用io.swagger.v3.oas.annotations.parameters.RequestBody 这是我的代码,我希望生成一个规范
@RequestBody(required={true | false})
似乎只将body中的所有参数设置为(not)required。
另一方面,@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);
}
我怎么能只需要四种请求类型的特定参数呢?重要
- 到给定端点的请求正文不应超过1个李>
- 请求主体主要是一个JSON对象。因此,为了使主体中的某些属性成为强制性的,建议使用验证api
- 有2个
注释。一个来自Spring框架@RequestBody
,另一个来自org.springframework.web.bind.annotation.RequestBody
io.swagger.v3.oas.annotations.parameters.RequestBody
io.swagger.v3.oas.annotations.parameters.RequestBody
,您仍然需要使用org.springframework.web.bind.annotation.RequestBody
来接收实际对象
按如下方式重构代码对您的情况应该很有帮助
控制器类
@GetMapping(“/fstVector”)
公众响应fstV(
//我们通常使用@RequestParam作为查询参数。查询参数通常是可选的,因此@Parameter的“required”属性默认为“false”
@参数@RequestParam最终向量v1,
//如果必须传递参数,则将@Parameter设置为TRUE。
@参数(必需=true)@RequestParam最终向量v2
) {
返回新的响应状态(v1,HttpStatus.OK);
}
@后映射(“/fstVector”)
公共响应fstVPost(
//默认情况下,RequestBody对象是“必需的”。要使其成为可选对象,请添加“(必需=false)”
@org.springframework.web.bind.annotation.RequestBody//Spring
@io.swagger.v3.oas.annotations.parameters.RequestBody//swagger
@Valid//Bean验证以确保传入对象是否有效
最终向量v1
) {
返回新的响应状态(v1,HttpStatus.OK);
}
对于域对象,重构DTO,如下所示
DTO
@Schema(description=“My DTO”)
类向量{
//以下属性是必需的
@NotNull
@参数(description=“my first attribute”,required=true)
字符串属性1;
//下面的属性是可选的
@参数(description=“my second attribute”,required=false)
字符串属性2;
}
重要
- 到给定端点的请求正文不应超过1个李>
- 请求主体主要是一个JSON对象。因此,为了使主体中的某些属性成为强制性的,建议使用验证api
- 有2个
注释。一个来自Spring框架@RequestBody
,另一个来自org.springframework.web.bind.annotation.RequestBody
io.swagger.v3.oas.annotations.parameters.RequestBody
io.swagger.v3.oas.annotations.parameters.RequestBody
,您仍然需要使用org.springframework.web.bind.annotation.RequestBody
来接收实际对象
按如下方式重构代码对您的情况应该很有帮助
控制器类
@GetMapping(“/fstVector”)
公众响应fstV(
//我们通常使用@RequestParam作为查询参数。查询参数通常是可选的,因此@Parameter的“required”属性默认为“false”
@参数@RequestParam最终向量v1,
//如果必须传递参数,则将@Parameter设置为TRUE。
@参数(必需=true)@RequestParam最终向量v2
) {
返回新的响应状态(v1,HttpStatus.OK);
}
@后映射(“/fstVector”)
公共响应fstVPost(
//默认情况下,RequestBody对象是“必需的”。要使其成为可选对象,请添加“(必需=false)”
@org.springframework.web.bind.annotation.RequestBody//Spring
@io.swagger.v3.oas.annotations.parameters.RequestBody//swagger
@Valid//Bean验证以确保传入对象是否有效
最终向量v1
) {
返回新的响应状态(v1,HttpStatus.OK);
}
对于域对象,重构DTO,如下所示
DTO
@Schema(description=“My DTO”)
类向量{
//以下属性是必需的
@NotNull
@参数(description=“my first attribute”,required=true)
字符串属性1;
//下面的属性是可选的
@参数(description=“my second attribute”,required=false)
字符串属性2;
}
理想情况下,GET请求不应采用请求正文(直到您确实想要发送巨大对象),POST请求不应返回OK
理想情况下,GET请求不应采用请求正文(直到您确实想要发送巨大对象),POST请求不应返回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'