Dynamically changing the @ResponseStatus in annotation driven Spring MVC

前端 未结 3 1282
梦如初夏
梦如初夏 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:35

    You cannot set multiple status value for @ResponseStatus. One approach I can think of is to use @ExceptionHandler for response status which is not HttpStatus.OK

    @RequestMapping(value =  "login.htm", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView login(@ModelAttribute Login login) {
        if(loginIsValidCondition) {
            //process login
            //.....
            return new ModelAndView(...);
        }
        else{
            throw new InvalidLoginException();
        }
    }
    
    @ExceptionHandler(InvalidLoginException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ModelAndView invalidLogin() {
        //handle invalid login  
        //.....
        return new ModelAndView(...);
    }
    

提交回复
热议问题