javaspringspring-bootrest

REST API url and parameter naming with in Spring Boot?


For Spring Boot projects, I have seen some different usages and want to clarify the following points.

1- As far as I know, the following usage in the url with kebab-case is better than camelCase. Is it true?

@RequestMapping("/api/v1/fraud-check")

2- On the other hand, if we use parameter, then I think it is better to us camelCase in the url as shown below (attention that I use camelCase only for parameters, the rest (security-check) is kebab-case). Is this a proper usage?

@GetMapping("/security-check/{customerId}/{jobId}")
public ResponseEntity<List<Customer>> getCustomerByDepartmentIdAndJobId(
                                          @PathVariable int customerId,
                                          @PathVariable int jobId
) {

}

On the other hand, there are some cases when both parameter are the same type e.g. getById() and getByReferenceNumber(). In this case the following url mappings cannot be used:

@GetMapping("/{userId}")

and

@GetMapping("/{referenceId}")

In this case, is it a good practice to add an extra parameter ('references') to reference one as shown below:

@GetMapping("/references/{referenceId}")

What would you suggest?


Solution

  • Your point regarding casing is completely valid and it goes well with how APIs are written generally.

    We have a great article that will help you get started with what are the best practices for REST apis: https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/

    At the end regarding your end use case of endpoint:

    @GetMapping("/references/{referenceId}")
    

    This is really subjective and will depend on your use case. For instance, adding "references" will also depend on your class-level requestmapping string which speaks about what is the domain of the controller class.

    Suppose I have Controller specifically written for Reference I can go like:

    @RequestMapping("/api/v1/reference")
    @Controller
    class ReferenceController {
    
    @GetMapping("/{id}")
    ... getReferenceById (@PathVariable int id) {}
    }
    

    Hope this clarifies the point and helps. Thanks.