I need to throw a 405 for all the http methods that are not defined in my controller. I am using spring boot is there a generic way to write it so that i do not have to add a method on my controller to read all the HTTP methods currently i do not get any response but it throws a 200 OK. Below is my controller that only has GET and PUT. I want to throw a 405 for all other methods.
@RestController("CardController")
@RequestMapping("/user/v1")
public class CardController {
@Autowired
ICardService iCardService;
@RequestMapping(value = "/{cardHolderId}/cards", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
@ResponseBody
public AllCardsVO getCards(@PathVariable("cardHolderId") final String id) {
return jsonObj;
}
@RequestMapping(value = "/{cardHolderId}/cards/{cardId}", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
@ResponseBody
public CardVO getCard(@PathVariable("cardHolderId") final String cardHolderId,
@PathVariable("cardId") final String cardId){
return jsonObj;
}
@RequestMapping(value = "/{cardHolderId}/cards/{cardId}", produces = "application/json; charset=utf-8", method = RequestMethod.PUT)
@ResponseBody
public CardVO putCard(@PathVariable("cardHolderId") final String cardHolderId,
@PathVariable("cardId") final String cardId, @RequestBody final RequestVO requestVO) {
return jsonObj;
}
This is what I wrote in the controller but it does not work for PATCH.
@RequestMapping(value = "/**", produces = "application/json; charset=utf-8", method = { RequestMethod.OPTIONS,
RequestMethod.DELETE, RequestMethod.PATCH, RequestMethod.POST })
@ResponseBody
public void options(HttpServletResponse response) {
throw new MethodNotAllowedException();
}
Is there a generic way i can do this in spring boot some sort of configuration override or service having this method in every api controller does not seem correct. IF this is the only way how do i get the PATCH working. I am getting a response for PATCH even though i do not have that defined.
ANy help is appreciated.