我为一个URL实现了两种不同的PostMapping。 API通过标头中的不同条目来区分后请求。我将Swagger注释用于API文档。不幸的是,Swagger-UI仅记录了两个映射之一。
我知道最佳实践是使用其他网址。 但是是否可以告诉Swagger列出控制器的两个PostMapping?
@RestController
@RequestMapping(AgreementController.BASE_URL)
@Api("Persists and checks agreements.")
public class AgreementController {
public static final String BASE_URL = "/api/v1/agreements";
private final AgreementService agreementService;
public AgreementController(AgreementService agreementService) {
this.agreementService = agreementService;
}
@PostMapping(headers = "action=post-agreement", consumes = "application/json",
produces = "application/json")
public ResponseEntity<DataObject> postAgreement(@RequestBody DataObject payload) {
agreementService.persistAgreement(payload);
return ResponseEntity.status(HttpStatus.CREATED).body(payload);
}
@PostMapping(headers = "action=get-agreement", consumes = "application/json")
public ResponseEntity getAgreement(@RequestBody DataObject payload) {
Boolean found = agreementService.findSingleAgreement(payload);
if (Boolean.TRUE.equals(found)) {
return new ResponseEntity(HttpStatus.OK);
} else {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}
}
Swagger配置:
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(regex("/api/.*"))
.build()
.apiInfo(metaData());
}