4

In Spring Boot is it possible to have the same endpoint with and without @RequestParam

A would look like this:

@GetMapping(value = "/billing-codes")
public ResponseEntity getBillingCodes(
        @AuthenticationPrincipal User loggedInUser,
        @RequestParam(name = "billingCodeId") String billingCodeId,
        @RequestParam(name = "direction") String direction,
        @RequestParam(name = "limit") String limit) {

B would look like this:

@GetMapping(value = "/billing-codes")
public ResponseEntity getBillingCodes(
        @AuthenticationPrincipal User loggedInUser) {

The requests would look different

A request

/billing-codes?billingCodeId=&direction=&limit

B request

/billing-codes

It is possible to test if the names of the request parameters are part of the request? Just testing if the request parameters are null or empty won't work since I will have case where they are and should still be processed in A.

g3blv
  • 3,877
  • 7
  • 38
  • 51
  • May be you can try having different HttpMethods one for GET and one for POST but still its upto your design. – Barath Aug 14 '17 at 19:29

1 Answers1

4

No, you can only have one method by couple of URL and HTTP Method in the same Controller. You'll probably need to use one method with required = false :

public ResponseEntity getBillingCodes(
        @AuthenticationPrincipal User loggedInUser,
        @RequestParam(name = "billingCodeId", required = false) String billingCodeId,
        @RequestParam(name = "direction"m required = false) String direction,
        @RequestParam(name = "limit", required = false) String limit) {}
Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60