1. 程式人生 > >Spring MVC 異常處理 - ResponseStatusExceptionResolver

Spring MVC 異常處理 - ResponseStatusExceptionResolver

執行 代碼 pin ces val col resolv use turn

作用在類和方法上面 更改返回的代碼和錯誤消息

類上 通過throw new UserName***Exception()拋出

@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="用戶名和密碼不匹配!")
public class UserNameNotMatchPasswordException extends RuntimeException{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    
}

方法上 方法執行通過,但是返回會有錯誤,因為配置了@ResponseStatus

@ResponseStatus(reason="測試",value=HttpStatus.NOT_FOUND)
    @RequestMapping("/testResponseStatusExceptionResolver")
    public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
        if(i == 13){
            throw new UserNameNotMatchPasswordException();
        }
        System.
out.println("testResponseStatusExceptionResolver..."); return "success"; }

Spring MVC 異常處理 - ResponseStatusExceptionResolver