SpringBoot /error Error Page status 为 999 的问题

配置

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

附言 1  ·  2个月前

问题已解决

public class ErrorPageController implements ErrorController {
...

    @Override
    public String getErrorPath() {
        return "/error/404";
    }
}
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!