#需求:通过swagger实现接口文档
#场景:一个接口,有2个流程,2个流程需要的参数不一样,想通过swagger实现选一个流程,能知道这个流程里面需要什么参数。
例如:流程1需要:name 名称, type 类型:1,2,3
流程2需要:name 名称, type 类型:a,b,c,status 状态:t,f
我通过下面的代码实现,发现接口文档里面不能正确的显示出来所需的参数。
#硬性要求:只能一个接口
#还有一种方法,就是把TypeA和TypeB作为成员放到一个大类里面, 但是因为我有大概10多个类型,放到一个大类里面显得很傻,想看看有没有别的方法,在线请教
@Data
public class TypeA {
@Schema(description = "姓名", required = true)
private String name;
@Schema(description = "类型:1-普通, 2-中等,3-高级", )
private Integer type;
···
此处忽略20个参数
}
@Data
public class TypeB {
@Schema(description = "姓名", required = true)
private String name;
@Schema(description = "类型:a-低速, b-普通,c-高速")
private String type;
@Schema(description = "t-成功,f-失败")
private String status;
···
此处忽略20个参数
}
@RestController
@RequestMapping("/api")
public class TestGroupContorller {
@PostMapping("/your-endpoint")
@Operation(summary = "Example endpoint with grouped parameters",
requestBody = @RequestBody(content = {
@Content(mediaType = "application/json", schema = @Schema(oneOf = {TypeA.class, TypeB.class}))
}),
responses = {
@ApiResponse(description = "Successful response", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseMessage.class)))
})
public ResponseEntity<ResponseMessage> yourEndpoint(@org.springframework.web.bind.annotation.RequestBody RequestType request) {
// Your endpoint implementation
return ResponseEntity.ok(new ResponseMessage("Success"));
}
static class ResponseMessage {
private String message;
public ResponseMessage(String message) {
this.message = message;
}
}
}