
com.cloud.boot.exception.errorPage.BaseErrorPage Maven / Gradle / Ivy
package com.cloud.boot.exception.errorPage;
import com.cloud.boot.applicationcontext.SystemInfo;
import com.cloud.boot.applicationcontext.init.BeanContextHolder;
import com.cloud.boot.exception.ExceptionCustomNotFoundPage;
import com.cloud.boot.util.TypeConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import java.util.Map;
/**
* 方法1:Spring Boot 将所有的错误默认映射到/error, 实现ErrorController
* @author wg
*
*/
@Controller
@RequestMapping("/error")
public class BaseErrorPage implements ErrorController {
@Autowired
private ErrorAttributes errorAttributes;
public String getErrorPath() {
Map map = errorAttributes.getErrorAttributes(new ServletWebRequest(BeanContextHolder.getRequest()), true);
return TypeConvert.isNull(map.get("path")) ? "" : map.get("path").toString();
}
@RequestMapping
public String error() throws ExceptionCustomNotFoundPage {
throw new ExceptionCustomNotFoundPage("http://" + SystemInfo.getHost() + ":" + SystemInfo.getPort() + getErrorPath() + " 访问地址不存在");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy