In Spring 3 is it possible to dynamically set the reason of @ResponseStatus?

后端 未结 7 1947
别跟我提以往
别跟我提以往 2021-02-05 02:01

I have a custom exception class annotated to return a given HttpStatus:

@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason=\"Invalid parameter\")
         


        
7条回答
  •  遇见更好的自我
    2021-02-05 02:12

    You can use HttpServletResponse's sendError function to achieve that.
    Here is an example of how to use it:

    @RequestMapping(value = "some/url", method = RequestMethod.POST)
    public void doAction(final HttpServletResponse response) throws IOException {
      response.sendError(HttpStatus.BAD_REQUEST.value(), "custom error message");
    }
    

提交回复
热议问题