I have a spring boot application that has a spring MVC controller. I am trying to execute my rest api version using the Accept header.
Below is what my controller looks like:
RestController @RequestMapping(value = "/private/") public class AppleController { private final AppleService appleService; public AppleController(AppleService appleService) { this.appleService = appleService; } @GetMapping(value = "apples/{id}", produces = "application/json; v=1.0", headers = "Accept=application/json; v=1.0") public ResponseEntity getByappleId(@PathVariable("id") Long appleId) { System.out.println("version1"); GetByappleIdResponse response = appleService.findByappleId(appleId); return new ResponseEntity<>(response, HttpStatus.OK); } @GetMapping(value = "apples/{id}", produces = "application/json; v=2.0", headers = "Accept=application/json; v=2.0") public ResponseEntity getByappleId2(@PathVariable("id") Long appleId) { System.out.println("version2"); GetByappleIdResponse response = appleService.findByappleId2(appleId); return new ResponseEntity<>(response, HttpStatus.OK); }
Regardless of the version that I pass in the Accept header when calling the API, the getByappleId method is always called, so only version 1 response is returned.
Is there something wrong with my controller?
spring-boot spring-mvc spring-restcontroller
Prabhakar d
source share