springboot自定义404页面

添加配置类

java 复制代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.coyote.UpgradeProtocol;
import org.springframework.beans.BeansException;
import  org.springframework.beans.factory.config.BeanPostProcessor;
import  org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import  org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import  org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import lombok.extern.slf4j.Slf4j;
@ConditionalOnClass({ Servlet.class, Tomcat.class,  UpgradeProtocol.class,  TomcatWebServerFactoryCustomizer.class })
@Component
@Slf4j
public class TomcatCustomizerBeanPostProcessor implements  BeanPostProcessor {
     @Override
     public Object postProcessAfterInitialization(Object  bean, String beanName) throws BeansException {
          if (bean instanceof  ConfigurableTomcatWebServerFactory) {
              ConfigurableTomcatWebServerFactory  configurableTomcatWebServerFactory =  (ConfigurableTomcatWebServerFactory) bean;
              
              addTomcat404CodePage(configurableTomcatWebServerFactory);
          }
          return  BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
     }
     private static void  addTomcat404CodePage(ConfigurableTomcatWebServerFactory  factory) {
          factory.addContextCustomizers((context) -> {
              String tomcatTempPath =  context.getCatalinaBase().getPath();
              log.info("tomcat目录:{}", tomcatTempPath);
              String path = tomcatTempPath + "/404.html";
              ClassPathResource cr = new  ClassPathResource("404.html");
              if (cr.exists()) {
                   File file404 = new File(path);
                   if (!file404.exists()) {
                        try {
                             FileCopyUtils.copy(cr.getInputStream(), new FileOutputStream(file404));
                        } catch (IOException e) {
                             e.printStackTrace();
                        }
                   }
              }
              ErrorReportValve valve = new  ErrorReportValve();
              valve.setProperty("errorCode.404", path);
              valve.setShowServerInfo(false);
              valve.setShowReport(false);
              valve.setAsyncSupported(true);
              context.getParent().getPipeline().addValve(valve);
          });
     }
}

在resources文件夹下直接添加404.html文件,再次启动测试,发现404页面已修改

相关推荐
lizhongxuan4 小时前
AIOPS 的自治运维与可验证进化机制
后端
Warson_L7 小时前
python - set/tuple/dict quiz
后端
IT_Octopus7 小时前
Spring Boot 实战:@PostConstruct + Caffeine 缓存初始化与定时刷新
spring boot·后端·缓存
swipe8 小时前
从本地开发到生产部署:用 Docker Compose 跑通 NestJS、MySQL 与 Milvus
后端·langchain·llm
码事漫谈8 小时前
SenseNova Skills Studio:为商汤SenseNova U1打造的本地办公技能包
后端
zhangxingchao8 小时前
AI应用开发七:可以替代 RAG 的技术
前端·人工智能·后端
Java面试题总结8 小时前
java高频面试题(2026最新)
java·开发语言·jvm·数据库·spring·缓存
苦逼的猿宝9 小时前
学生心理咨询评估系统
java·毕业设计·springboot·计算机毕业设计
隔窗听雨眠9 小时前
doctype、charset、meta如何控制整个渲染流水线
java·服务器·前端
excel9 小时前
🧠 Prisma 表名大写 vs SQL 导出小写问题深度解析(附踩坑与解决方案)
前端·后端