Spring 注解@ExceptionHandler和@ResponseStatus

无论是普通的WEB项目,还是用SpringMVC实现的restful服务,都曾经历过下面两个问题:

1.1 Controller中处理异常响应报文

@PostMapping(path = "/selectByAcctcode")
public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
 
log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
 
MerAccountQueryResponse response = new MerAccountQueryResponse();
 
try {
Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
response = merAccountService.selectByAcctcode(request, pageable);
// 返回成功报文
MessageUtil.createCommMsg(response);
} catch (BizException e) {
log.error(Constants.BUSINESS_ERROR, e);
// 组织错误报文
MessageUtil.errRetrunInAction(response, e);
} catch (Exception ex) {
log.error(Constants.EXCEPTION_ERROR, ex);
// 组织错误报文
MessageUtil.createErrorMsg(response,ex);
}
log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
return response;
}

当你有100个接口的时候,就得重复100次,如果你觉得代码量也就那么点,copy就copy吧,反正我是代码的搬运工,只是你有曾想过我可以抽取出来吗?

1.2 Controller中处理异常

我们在写Controller的时候,如果没有出现过异常固然没问题,但一旦出现异常了,如果你处理了,那就需要你手动指定跳转到事先定义好的界面,如果你没处理,那将得到是一个非常丑陋的界面,如下:

Spring 注解@ExceptionHandler和@ResponseStatus
如何避免这种问题呢???
@ExceptionHandler

2.1返回自定义错误界面

@Controller
@RequestMapping(value = "exception")
public class ExceptionHandlerController {
 
@ExceptionHandler({ ArithmeticException.class })
public String handleArithmeticException(Exception e) {
e.printStackTrace();
return "error";
}
 
@RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
@ResponseBody
public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
System.out.println(10 / id);
return id.toString();
}
}

当访问exception/e/0的时候,会抛出ArithmeticException异常,@ExceptionHandler就会处理并响应error.jsp

@Controller
@RequestMapping(value = "exception")
public class ExceptionHandlerController {
 
@ExceptionHandler({ ArithmeticException.class })
@ResponseBody
public String handleArithmeticException(Exception e) {
e.printStackTrace();
JSONObject jo = new JSONObject();
jo.put("resCode","999999");
jo.put("resMsg","系统异常");
return jo.toString();
}
 
@RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
@ResponseBody
public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
System.out.println(10 / id);
return id.toString();
}
}

当然实际项目中,并不会像我这里写的这么简陋,我这里只是抛砖引玉,给你一个思路。

@ResponseStatus
在实际项目中,可能碰到这种情况,我们提供的服务,调用方并不需要json报文中的消息,调用方只关注响应码,比如200,代表调用正常;404,代表请求资源不存在;502,代表系统异常。。。等等。我们又该如何去做?

@ResponseStatus在自定义异常中使用

package com.somnus.exception;
 
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
 
@ResponseStatus(value=HttpStatus.BAD_GATEWAY)
public class HttpStatusException extends RuntimeException {
 
private static final long serialVersionUID = 1L;
 
public HttpStatusException() {
super();
}
 
public HttpStatusException(String message, Throwable cause) {
super(message, cause);
}
 
public HttpStatusException(String message) {
super(message);
}
 
public HttpStatusException(Throwable cause) {
super(cause);
}
 
}
@Controller
@RequestMapping(value = "status")
public class ResponseStatusController {
 
@RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
@ResponseBody
public String status(@PathVariable(value = "id") Integer id){
if(id % 2 != 0){
throw new HttpStatusException();
}
return id.toString();
}
}

效果如下:
Spring 注解@ExceptionHandler和@ResponseStatus
另外这里不得不提一点需要注意的,不要轻易把@ResponseStatus修饰目标方法,因为无论它执行方法过程中有没有异常产生,用户都会得到异常的界面,而目标方法正常执行。

package com.somnus.controller;
 
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
 
import com.somnus.exception.HttpStatusException;
 
@Controller
@RequestMapping(value = "status")
public class ResponseStatusController {
 
/**
* ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
* @param id
* @return
*/
@RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
@ResponseStatus(value=HttpStatus.BAD_GATEWAY)
@ResponseBody
public String status2(@PathVariable(value = "id") Integer id){
System.out.println(10 / id);
return id.toString();
}
 
}

可以看到哪怕是响应2了,但是响应码其实还是502
Spring 注解@ExceptionHandler和@ResponseStatus

[email protected]配合@ExceptionHandler使用

@Controller
@RequestMapping(value = "exception")
public class ExceptionHandlerController {
 
@ExceptionHandler({ NullPointerException.class })
@ResponseStatus(value=HttpStatus.NOT_FOUND)
public void handleNullPointerException(Exception e) {
e.printStackTrace();
}
 
@RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
@ResponseBody
public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
return list.get(id);
}
}

当我们抛出NullPointerException异常的时候会发生什么呢

Spring 注解@ExceptionHandler和@ResponseStatus
总结:

  • 当一个Controller中有多个@ExceptionHandler注解出现时,那么异常被哪个方法捕捉呢?这就存在一个优先级的问题,@ExceptionHandler的优先级是:在异常的体系结构中,哪个异常与目标方法抛出的异常血缘关系越紧密,就会被哪个捕捉到

  • @ExceptionHandler这个只会是在当前的Controller里面起作用,如果想在所有的Controller里面统一处理异常的话,可以用@ControllerAdvice来创建一个专门处理的类,我们在下一篇做介绍。