统一异常处理demo
public class BizException extends RuntimeException {
private String retCode;
private String retMessage;
public BizException ( ) {
super ( ) ;
}
public BizException ( String retCode, String retMessage) {
this . retCode = retCode;
this . retMessage = retMessage;
}
public String getRetCode ( ) {
return retCode;
}
public String getRetMessage ( ) {
return retMessage;
}
}
@RestControllerAdvice ( annotations = RestController . class )
public class ControllerExceptionHandler {
@ExceptionHandler ( BizException . class )
@ResponseBody
public String handler ( BizException e) {
return "异常编码:" + e. getRetCode ( ) + "------------>" + e. getRetMessage ( ) ;
}
}
@Service
public class UserServiceImpl {
public void queryUserInfo ( ) {
throw new BizException ( "001" , "测试异常" ) ;
}
}
@Resource
UserServiceImpl userService;
@RequestMapping ( "/query" )
public void queryUserInfo ( ) {
System . out. println ( "iiiiii" ) ;
int a = 0 ;
userService. queryUserInfo ( ) ;
}