2

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.

Grinish Nepal
  • 3,037
  • 3
  • 30
  • 49
  • Why not live with the standard 404 ? – Marged Jan 11 '16 at 21:43
  • Business requirement.... also I get 200 OK for some with blank response rather than 404 without the code that I have written in the controller. – Grinish Nepal Jan 11 '16 at 21:52
  • I guess it's the default behavior. Spring Boot will automatically return 405 – Ali Dehghani Jan 11 '16 at 22:01
  • 1
    You should get a 405 at least for every other method besides OPTIONS. If you don't, you presumably have anything in your application configuration, which influences that. Regarding the OPTIONS request, if you want to disable processing of OPTIONS requests, see the following question: http://stackoverflow.com/questions/33331042/how-to-handle-http-options-requests-in-spring-boot – dunni Jan 11 '16 at 22:35
  • @dunni I think everything else except PATCH and OPTIONS by default are giving me 405. Any suggestions on PATCH i get a response back on it with 200OK also with dispatcherservlet overriding i get a 200 ok for options with blank response. – Grinish Nepal Jan 19 '16 at 18:06

0 Answers0