一、全局异常处理类
package com.example.demo.filter;
import org.apache.shiro.authz.AuthorizationException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.exceptions.JedisConnectionException;
@ControllerAdvice
public class ExceptionFilter {
@ExceptionHandler
@ResponseBody
public String ErrorHandler(Exception e) {
return "未知异常!原因是" + e;
}
@ExceptionHandler
@ResponseBody
public String ErrorHandler(AuthorizationException e) {
return "没有通过权限验证!";
}
@ExceptionHandler
@ResponseBody
public String ErrorHandler(JedisConnectionException e) {
return "redis连接失败!";
}
@ExceptionHandler
@ResponseBody
public String ErrorHandler(MyBatisSystemException e) {
return "数据库连接失败!";
}
}
二、错误页面配置类
registerErrorPages.java
package com.example.demo.config;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400");
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
registry.addErrorPages(error400Page, error401Page, error404Page, error500Page);
}
}
ErrorPageController.java
package com.example.demo.config;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/error")
public class ErrorPageController {
@RequestMapping("/{code}")
public String toErrorPage(@PathVariable int code) {
return "error/" + code;
}
}
三、URL过滤器
package com.example.demo.filter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
@WebFilter(filterName = "MyFilter", urlPatterns = "/*")
public class UrlFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
// HttpServletResponse httpResponse = (HttpServletResponse) response;
String url = httpRequest.getRequestURL().toString();
String qryString = httpRequest.getQueryString();
System.out.println(url + (qryString == null ? "" : "?" + qryString));
chain.doFilter(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
}
}