问题:
举例:
比如开发用户注册的接口:
比如:用户名不为空,密码格式不对
{status:501,msg:"用户名不为空"}
{status:502,msg:"密码格式不对"}
具体异常优先级要高于大异常。
在try/catch的具体异常一定写在大异常上面
try {
Connection con = null;
PreparedStatement preparedStatement = con.prepareStatement("");
}catch (SQLException sqlex){
sqlex.printStackTrace();
} catch(Exception exx){
}
package com.kuangstudy.web.error.config2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ControllerAdvice
public class GlobalExceptionControllerHandler {
/**
* 拦截所有程序异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=Exception.class)
public String errorHandler(HttpServletRequest request,Exception ex){
return "err2/noError";
}
/**
* SQLException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=SQLException.class)
public String errorHandlerSQL(HttpServletRequest request,Exception ex){
return "err2/sqlError";
}
/**
* MyException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=MyException.class)
public String errorHandlerMy(HttpServletRequest request,Exception ex){
return "err2/myError";
}
}
@ControllerAdvice 和 @RestControllerAdvice底层原理是:AOP 主要用于开启全局异常处理一种机制,对后面的统一返回,统一异常处理,统一参数注入都会用这个@ControllerAdvice。
有了@ControllerAdvice 为什么还出现@RestControllerAdvice,其实和@Controller和@RestController一个道理。因为在程序开发中,不仅仅只有页面返回处理。如果单体项目,有freemarker 和 thymeleaf的话其实使用@ControllerAdvice做统一异常处理能够满足错误处理机制。
如果在有freemarker 和 thymeleaf的使用@RestControllerAdvice 会怎么样呢?
package com.kuangstudy.web.error.config2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionControllerHandler {
/**
* 拦截所有程序异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=Exception.class)
@ResponseBody
public String errorHandler(HttpServletRequest request,Exception ex){
return "err2/noError";
}
/**
* SQLException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=SQLException.class)
@ResponseBody
public String errorHandlerSQL(HttpServletRequest request,Exception ex){
return "err2/sqlError";
}
/**
* MyException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=MyException.class)
@ResponseBody
public String errorHandlerMy(HttpServletRequest request,Exception ex){
return "err2/myError";
}
}
@ControllerAdvice 和 @RestControllerAdvice的区别:
package com.kuangstudy.web.error.config2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ControllerAdvice
//@RestControllerAdvice
public class GlobalExceptionControllerHandler {
/**
* 拦截所有程序异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=Exception.class)
public ModelAndView errorHandler(HttpServletRequest request, Exception ex ){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("err2/noError");
modelAndView.addObject("status", HttpStatus.INTERNAL_SERVER_ERROR);
modelAndView.addObject("msg",ex.getMessage());
modelAndView.addObject("url",request.getRequestURL().toString());
return modelAndView;
}
/**
* SQLException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=SQLException.class)
public String errorHandlerSQL(HttpServletRequest request,Exception ex){
return "err2/sqlError";
}
/**
* MyException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=MyException.class)
@ResponseBody
public String errorHandlerMy(HttpServletRequest request,Exception ex){
return "err2/myError";
}
}
package com.kuangstudy.web.error.config2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class GlobalExceptionControllerHandler {
/**
* 拦截所有程序异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=Exception.class)
public Map<String,Object> errorHandler(HttpServletRequest request, Exception ex ){
Map<String,Object> map = new HashMap<>();
map.put("status",HttpStatus.INTERNAL_SERVER_ERROR);
map.put("msg",ex.getMessage());
map.put("url",request.getRequestURL().toString());
return map;
}
/**
* SQLException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=SQLException.class)
public Map<String,Object> errorHandlerSQL(HttpServletRequest request,Exception ex){
Map<String,Object> map = new HashMap<>();
map.put("status",601);
map.put("msg",ex.getMessage());
map.put("url",request.getRequestURL().toString());
return map;
}
/**
* MyException异常
* @param request
* @param ex
* @return
*/
@ExceptionHandler(value=MyException.class)
@ResponseBody
public Map<String,Object> errorHandlerMy(HttpServletRequest request,Exception ex){
Map<String,Object> map = new HashMap<>();
map.put("status",602);
map.put("msg",ex.getMessage());
map.put("url",request.getRequestURL().toString());
return map;
}
}
原因如:
命名方式:
无论用那种,都是一种面向封装的思想。
理解:全局异常处理就很像另外一个controller,由异常去触发