SpringBoot3.2.3+Jetty12中嵌套Jar资源的DefaultServlet 404问题排查
SpringBoot 3.2.3 + Jetty 12.0.6 下DefaultServlet嵌套Jar资源访问404问题
已完成向SpringBoot 3.2.3和Jetty 12.0.6的迁移,遇到DefaultServlet的baseResource注册问题——相同配置在SpringBoot 2.7.X及旧版Jetty环境下可正常运行。
自定义JettyServletWebServerFactory添加配置、注册Servlet后,将baseResource定义为嵌套Jar资源时,使用java -jar启动服务会出现404异常,但用IntelliJ IDEA启动服务却能正常工作,看起来baseResource未被作为嵌套资源处理。
源代码
package com.example.demo; import jakarta.servlet.ServletRegistration; import org.eclipse.jetty.ee10.servlet.DefaultServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import java.net.URL; @SpringBootApplication public class DemoApplication implements WebServerFactoryCustomizer<JettyServletWebServerFactory> { private static final Logger LOGGER = LoggerFactory.getLogger(DemoApplication.class); public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void customize(JettyServletWebServerFactory factory) { DefaultServlet defaultServlet = new DefaultServlet(); factory.addInitializers(ctx -> { ServletRegistration sr = ctx.addServlet("swagger-ui", defaultServlet); sr.addMapping("/swagger-ui/*"); URL externalForm = DemoApplication.class.getResource("/static/swagger-ui/"); LOGGER.info("### resources: " + externalForm.toExternalForm()); sr.setInitParameter("baseResource", externalForm.toExternalForm()); sr.setInitParameter("pathInfoOnly", "true"); }); LOGGER.info("WebServer customized with swagger-ui"); } }
pom.xml内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> <exclusions> <!-- Exclude the Tomcat dependency --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <artifactId>tomcat-embed-el</artifactId> <groupId>org.apache.tomcat.embed</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!-- Exclude the Tomcat dependency --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
内容的提问来源于stack exchange,提问作者lmetrak
相关产品推荐
相关产品推荐

