配置
server:
port: 8000
servlet:
context-path: "/api"
compression:
enabled: false
刚接触 SpringBoot
,HelloWolrd 已经实现。普通的 404 也处理成功了。但是 127.0.0.1:8000/api/error
一直都是 “Whitelabel Error Page”。尝试了过很多方法,还是无法解决。
我需要把状态 999
添加到 ErrorPageConifg
里面,但是我不知道该怎么添加。理论上拓展 HttpStatus
就可以了,但这个是只读的。
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jan 14 16:29:43 CST 2020
There was an unexpected error (type=None, status=999).
No message available
ErrorPageConifg.java
package cn.vlice.aox.AoxServer.config;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
@Configuration
public class ErrorPageConifg {
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
ErrorPage page500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
factory.addErrorPages(page404, page500);
}
};
}
}
ErrorPageController.java
package cn.vlice.aox.AoxServer.controller;
import cn.vlice.aox.AoxServer.data.Request;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/error")
public class ErrorPageController {
@RequestMapping("/999")
public Request toPage999() {
HttpStatus status = HttpStatus.valueOf(404);
return Request.error(status.getReasonPhrase(), status.value());
}
@RequestMapping("/404")
public Request toPage404() {
HttpStatus status = HttpStatus.valueOf(404);
return Request.error(status.getReasonPhrase(), status.value());
}
@RequestMapping("/500")
public Request toPage500() {
HttpStatus status = HttpStatus.valueOf(500);
return Request.error(status.getReasonPhrase(), status.value());
}
}
Violet_Ice 紫冰,一个忙于工作,没有时间录视频的 UP
问题已解决
public class ErrorPageController implements ErrorController {
...
@Override
public String getErrorPath() {
return "/error/404";
}
}
最后注意:光理论是不够的。这里顺便送大家一套2020最新JAVA架构项目实战教程及大厂面试题库,进我扣裙 :七吧伞吧零而衣零伞 (数字的谐音)转换下可以找到了,里面很多新JAVA架构项目,还有可以跟老司机交流
本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理