如何在 spring 控制器中使用枚举作为参数,该控制器使用 "spring" 语言扩展 swagger-codegen 生成的接口
How to use enum as parameter in spring controller that extends interface generated by swagger-codegen with "spring" language
假设我有一个用枚举模式定义的参数:
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
type: string
enum: [A, B, C]
responses:
200:
description: Success
content:
text/plain:
schema:
type: string
Swagger codegen 为 spring
语言生成以下 Java 界面:
@ApiOperation(value = "", nickname = "echoGet", notes = "", response = String.class, tags={ })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class) })
@RequestMapping(value = "/echo",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> echoGet(@NotNull @ApiParam(value = "", required = true, allowableValues = "A, B, C") @Valid @RequestParam(value = "someEnum", required = true) String someEnum) {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default EchoApi interface so no example is generated");
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
生成的界面中someEnum
的类型是字符串。如何用 "A"、"B"、"C" 值的 Java 枚举替换它?
在 components/schemas
部分定义您的枚举模式,并在参数定义中 $ref
定义它,如下所示。在这种情况下,Swagger Codegen 会将枚举生成为实际的 Java 枚举。
openapi: 3.0.0
...
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
$ref: '#/components/schemas/MyEnum'
...
components:
schemas:
MyEnum:
type: string
enum: [A, B, C]
向模式添加命名类型并使用它:
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
$ref: '#/components/schemas/MyEnum'
responses:
200:
description: Success
content:
text/plain:
schema:
type: string
components:
schemas:
MyEnum:
type: string
enum: [A,B,C]
假设我有一个用枚举模式定义的参数:
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
type: string
enum: [A, B, C]
responses:
200:
description: Success
content:
text/plain:
schema:
type: string
Swagger codegen 为 spring
语言生成以下 Java 界面:
@ApiOperation(value = "", nickname = "echoGet", notes = "", response = String.class, tags={ })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = String.class) })
@RequestMapping(value = "/echo",
produces = { "text/plain" },
method = RequestMethod.GET)
default ResponseEntity<String> echoGet(@NotNull @ApiParam(value = "", required = true, allowableValues = "A, B, C") @Valid @RequestParam(value = "someEnum", required = true) String someEnum) {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default EchoApi interface so no example is generated");
}
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
生成的界面中someEnum
的类型是字符串。如何用 "A"、"B"、"C" 值的 Java 枚举替换它?
在 components/schemas
部分定义您的枚举模式,并在参数定义中 $ref
定义它,如下所示。在这种情况下,Swagger Codegen 会将枚举生成为实际的 Java 枚举。
openapi: 3.0.0
...
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
$ref: '#/components/schemas/MyEnum'
...
components:
schemas:
MyEnum:
type: string
enum: [A, B, C]
向模式添加命名类型并使用它:
paths:
/echo:
get:
parameters:
- name: someEnum
in: query
required: true
schema:
$ref: '#/components/schemas/MyEnum'
responses:
200:
description: Success
content:
text/plain:
schema:
type: string
components:
schemas:
MyEnum:
type: string
enum: [A,B,C]