Dynamically changing the @ResponseStatus in annotation driven Spring MVC

前端 未结 3 1290
梦如初夏
梦如初夏 2021-02-01 03:01

I am really not sure if this is feasible using Spring 3.2 MVC.

My Controller has a method declared as below:

@RequestMapping(method = RequestMethod.GET)
         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 03:26

    @ResponseStatus(HttpStatus.OK) means that the request will return OK if the handling method returns normally (this annotation is redundant for this case, as the default response status is HttpStatus.OK). If the handler throws an exception, the annotation does not apply.

    How can I handle multiple response statuses depending on conditions in the same method?

    That question has already been asked.

    Can I change the Response Status on occurrence of any exception

    You have two choices. If the exception class is one of your own, you could annotate the exception class with @ResponseStatus. The other choice is to provide the controller class with an exception handler, annotated with @ExceptionHandler, and have the exception handler set the response status.

提交回复
热议问题