Let’s assume that I got two GET
method implementations
@RestController
@RequestMapping("/products/{id}")
public class ProductController {
@GetMapping(path = "/one")
public ResponseEntity<List<Product>> one(
@Parameter(/* doc */) @PathVariable String id,
@Parameter(/* doc */) @RequestParam(required = false) String query,
@Parameter(/* doc */) @RequestParam(defaultValue = "0") Integer offset,
@Parameter(/* doc */) @RequestParam(defaultValue = "10") Integer limit,
@Parameter(/* doc */) @RequestParam(required = false) List<String> categories) {
return ResponseEntity.noContent();
}
@GetMapping(path = "/two")
public ResponseEntity<List<Product>> two(
@Parameter(/* doc */) @PathVariable String id,
@Parameter(/* doc */) @RequestParam(required = false) String query,
@Parameter(/* doc */) @RequestParam(defaultValue = "0") Integer offset,
@Parameter(/* doc */) @RequestParam(defaultValue = "10") Integer limit,
@Parameter(/* doc */) @RequestParam(required = false) List<String> categories) {
return ResponseEntity.noContent();
}
}
I tried with creating record
record ProductParameters(
@Parameter(/* doc */) @PathVariable String id,
@Parameter(/* doc */) @RequestParam(required = false) String query,
@Parameter(/* doc */) @RequestParam(defaultValue = "0") Integer offset,
@Parameter(/* doc */) @RequestParam(defaultValue = "10") Integer limit,
@Parameter(/* doc */) @RequestParam(required = false) List<String> categories) {}
but in Swagger UI I got those one
and two
methods with single param productParameters
and just one entry in Schemas below… It looks like a request body with this “solution”.
Have you got any idea, how can I group request path/query parameters in Spring Boot to avoid duplications? When I got different project (not Spring based), I just use @BeanParam
for that.