我想要拦截404或500请求转到自己的页面上, 可是springboot无法拦截到。
看起来好像一个tomcat的error
而且404页面也不是springBoot封装的默认error处理页面
这是我的Controller拦截代码
package com.yukaiji.kjblog.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ErrorBlogController implements ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request){
//获取statusCode:401,404,500
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if(statusCode == 401){
return "/401";
}else if(statusCode == 404){
return "/404";
}else if(statusCode == 403){
return "/403";
}else{
return "/500";
}
}
@Override
public String getErrorPath() {
return "/error";
}
}
这是我的拦截器代码
package com.yukaiji.kjblog.config;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpClientErrorException;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class ViewConfig {
@ExceptionHandler(HttpClientErrorException.class)
public String handle(HttpServletRequest httpServletRequest, Exception e) {
System.out.println("111111111111111111111111");
return "404";
}
}