Re-use @ApiResponses error responses in the code
Re-use @ApiResponses error responses in the code
I use swagger with kotlin and have the code:
@MyApiGroup1
@RestController
interface MyController1 {
@ApiResponses(
ApiResponse(code = 200, message = "OK", response = SomeEntity::class),
ApiResponse(code = 400, message = "My error 1", response = MyError::class),
ApiResponse(code = 400, message = "My error 2", response = MyError::class),
ApiResponse(code = 409, message = "My error 3", response = MyError::class)
)
@RequestMapping("/", method = arrayOf(RequestMethod.GET))
fun getMyGet(param: Params): ResponseEntity<SomeEntity> {
//some magic
}
@ApiResponses(
ApiResponse(code = 201, message = "ADD"),
ApiResponse(code = 400, message = "My error 1", response = MyError::class),
ApiResponse(code = 400, message = "My error 2", response = MyError::class),
ApiResponse(code = 409, message = "My error 3", response = MyError::class)
)
@RequestMapping("/", method = arrayOf(RequestMethod.POST))
fun getMyPOST(param: Params) {
//some magic
}
}
@MyApiGroup2
@RestController
interface MyController2 {
//The same responses
@ApiResponses(
ApiResponse(code = 200, message = "OK", response = SomeEntity::class),
ApiResponse(code = 400, message = "My error 1", response = MyError::class),
ApiResponse(code = 400, message = "My error 2", response = MyError::class),
ApiResponse(code = 409, message = "My error 3", response = MyError::class)
)
@RequestMapping("/magic", method = arrayOf(RequestMethod.GET))
fun getMyGet2(param: Params): ResponseEntity<SomeEntity> {
//some magic2
}
}
As you can see, ApiResponses in both interfaces are the same.
Same ApiResponses are often found throughout the code. Often the difference between them is very minimal. I have to describe almost the same thing by each method.
0. How can I avoid this approach?
1. Can I standardize some errors and re-use them for different methods?
2. Can the Swagger himself generate ApiResponses based on errors in the code?
3. Can I re-use message = "My error 2" string?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How to DRY when using Swagger UI and the ApiResponses annotations (...)? should help.
– Roland
26 mins ago