Jboss7:Undertow Spring Boot 抛出 404

Posted

技术标签:

【中文标题】Jboss7:Undertow Spring Boot 抛出 404【英文标题】:Jboss7 : Undertow Spring Boot throwing 404 【发布时间】:2018-10-04 15:36:36 【问题描述】:

我正在开发一个 Spring Boot 应用程序。

我正在为 JBoss 构建一个战争文件

我的战争正在正确构建,我正在使用 *-war.original 作为 JBoss 的部署 部署似乎很好

但是当我调用控制器时,它会抛出 404 使用嵌入式服务器,它工作正常

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.my.service</groupId>
    <artifactId>my-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencyManagement>
        <dependencies>
            <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Swagger UI Depedency -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Oracle JDBC driver -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0</version>
        </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <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-undertow</artifactId>
        <scope>provided</scope>
    </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- <version>2.0.1.RELEASE</version> -->
                <!-- https://***.com/questions/43641664/why-spring-boot-generate-jar-or-war-file-with-original-extention  -->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <phase>package</phase>
                        <!-- https://***.com/questions/30325549/only-generate-one-war-during-package -->
                    </execution>
                </executions>
            </plugin>
            <!-- This configuration helps to generate war file without web.xml, spring 
                boot projects -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

SpringBootServletInitializer

@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
    return application.sources(MyApiApplication.class);
     

@Override
public void onStartup(ServletContext servletContext) throws ServletException
    super.onStartup(servletContext);

     

部署日志

12:01:09,605 INFO  [stdout] (ServerService Thread Pool -- 162)  ::
Spring Boot ::        (v2.0.1.RELEASE)
    2018-04-24 12:01:09,605 [ServerService Thread Pool -- 162] INFO  (AbstractLoggingWriter.java:71) -  :: Spring Boot ::       
(v2.0.1.RELEASE)
    12:01:09,605 INFO  [stdout] (ServerService Thread Pool -- 162)
    2018-04-24 12:01:09,605 [ServerService Thread Pool -- 162] INFO  (AbstractLoggingWriter.java:71) -
    12:01:09,669 INFO  [com.aerlingus.dei.subscriber.api.ServletInitializer] (ServerService
Thread Pool -- 162) Starting ServletInitializer on USER with PID 10692
(started by ADMIN in C:\pathto\jboss-eap-7.1\bin)
    2018-04-24 12:01:09,669 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - Starting ServletInitializer on CND7064JRN with
PID 10692 (started by ADMIN in C:\pathto\jboss-eap-7.1\bin)
    2018-04-24 12:01:09,669 [ServerService Thread Pool -- 162] DEBUG (JBossLog.java:134) - Running with Spring Boot v2.0.1.RELEASE, Spring
v5.0.5.RELEASE
    12:01:09,670 INFO  [com.aerlingus.dei.subscriber.api.ServletInitializer] (ServerService
Thread Pool -- 162) The following profiles are active: uat
    2018-04-24 12:01:09,670 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - The following profiles are active: uat
    12:01:09,686 INFO  [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext]
(ServerService Thread Pool -- 162) Refreshing
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5023cb50:
startup date [Tue Apr 24 12:01:09 BST 2018]; root of context hierarchy
    2018-04-24 12:01:09,686 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - Refreshing
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5023cb50:
startup date [Tue Apr 24 12:01:09 BST 2018]; root of context hierarchy
    12:01:09,805 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor]
(ServerService Thread Pool -- 162) JSR-330 'javax.inject.Inject'
annotation found and supported for autowiring
    2018-04-24 12:01:09,805 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - JSR-330 'javax.inject.Inject' annotation found
and supported for autowiring
    12:01:09,816 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 162) Initializing Spring embedded WebApplicationContext
    2018-04-24 12:01:09,816 [ServerService Thread Pool -- 162] INFO  (ServletContextImpl.java:360) - Initializing Spring embedded
WebApplicationContext
    12:01:09,816 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 162) Root WebApplicationContext:
initialization completed in 130 ms
    2018-04-24 12:01:09,816 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - Root WebApplicationContext: initialization
completed in 130 ms
    12:01:09,987 INFO  [org.springframework.boot.web.servlet.FilterRegistrationBean]
(ServerService Thread Pool -- 162) Mapping filter: 'errorPageFilter'
to: [/*]
    2018-04-24 12:01:09,987 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - Mapping filter: 'errorPageFilter' to: [/*]
    12:01:10,015 INFO  [com.aerlingus.dei.subscriber.api.ServletInitializer] (ServerService
Thread Pool -- 162) Started ServletInitializer in 0.683 seconds (JVM
running for 4712.635)
    2018-04-24 12:01:10,015 [ServerService Thread Pool -- 162] INFO  (JBossLog.java:134) - Started ServletInitializer in 0.683 seconds (JVM
running for 4712.635)
    12:01:10,021 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService
Thread Pool -- 162) Initializing Mojarra 2.2.13.SP3  for context
'/my-api'
    2018-04-24 12:01:10,021 [ServerService Thread Pool -- 162] INFO  (ConfigureListener.java:213) - Initializing Mojarra 2.2.13.SP3  for
context '/my-api'
    12:01:11,566 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 162) WFLYUT0021: Registered web context: '/my-api' for
server 'default-server'
    2018-04-24 12:01:11,566 [ServerService Thread Pool -- 162] INFO  (Host.java:220) - WFLYUT0021: Registered web context: '/my-api' for
server 'default-server'
    12:01:11,583 INFO  [org.jboss.as.server] (External Management Request Threads -- 3) WFLYSRV0010: Deployed "my-api.war" (runtime-name
: "my-api.war")
    2018-04-24 12:01:11,583 [External Management Request Threads -- 3] INFO  (DeploymentHandlerUtil.java:171) - WFLYSRV0010: Deployed
"my-api.war" (runtime-name : "my-api.war")

WEB-INF/jboss-web.xml

<jboss-web>
    <context-root>my-api</context-root>
</jboss-web>

类似这样 Getting "404 - Not found" error with Wildfly and springboot

【问题讨论】:

你好,请问有WEB-INF/jboss-web.xml的目的是什么? 我用它来设置上下文根,因为当我尝试使用 Spring Boot 属性文件作为上下文根时它不起作用。在我的情况下,Jboss 总是将部署名称视为上下文根,我的上下文根应该与部署名称不同,这就是我使用 jboss-web.xml 的原因。 如果我理解正确的话,如果你将 context-root 设置为 my-api,你将能够通过 localhost:8080/my- 访问它api,对吗?如果不通过 WEB-INF/jboss-web.xml 设置 context-root 会发生什么? 当我在 application.yaml 或 properties 中尝试 my-api 时,当我将 war 文件部署到 jboss 时它不起作用,因为当 CI 构建 war 文件时,文件的名称将是 my-api-version。战争。所以当我点击 localhost:8080/my-api 我得到一个 404 ,但是当点击 localhost:8080/my-api-version/ 它开始工作。所以我认为基于部署名称的 jboss 路由请求,你可以用 jboss-web.xml 覆盖它。可能有更好的方法。请自己尝试。我还使用了 deployment-structure.xml 来排除日志记录以禁用 jboss 日志记录并回退到 Spring Boot 日志记录。 @CongBangDO 这是我为什么使用 jboss-web.xml ***.com/a/50910256/1450401 的示例 【参考方案1】:

终于弄清楚我做错了什么,我有一个带有 main 方法的 Application 类,没有 @SpringBootApplicationSpringBootServletInitializer ,所以 Jboss 没有正确选择映射

解决方案是使用 main 方法制作 SpringBootServletInitializer 类。所以我改变了问题中提到的ServletInitializer

@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer

public static void main(String[] args) 
        SpringApplication.run(ServletInitializer.class, args);
    

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
    return application.sources(ServletInitializer.class);
     

@Override
public void onStartup(ServletContext servletContext) throws ServletException
    super.onStartup(servletContext);

     

之后我就可以到达终点了,

我为什么不早点这样做??

我使用生成的项目 SPRING INITIALIZR 带有战争包装,当 SPRING INITIALIZR 生成项目 ApplicationSpringBootServletInitializer 类在那里。 这两个类中Appliaction类都有main方法和@SpringBootApplication注解

最初编写集成测试时它不起作用,然后我将@SpringBootApplication注释更改为SpringBootServletInitializer并在SpringBootServletInitializer中编写一个main方法测试开始工作。但是我将 main 方法保留在 Appliaction 类中,这是导致此问题的根本原因。

自 Spring Initializer 以来,生成我假设的类,生成的所有类都是构建 war 所必需的。所以在生成 war 文件时,我保持 Application 类的生成方式没有 @SpringBootApplication 注释。我认为 Jboss 对它应该运行哪个主要方法感到困惑。并最终在到达终点时抛出 404 异常

【讨论】:

以上是关于Jboss7:Undertow Spring Boot 抛出 404的主要内容,如果未能解决你的问题,请参考以下文章

Undertow (JBoss 7) 在重定向时破坏/重新编码 URL 编码的参数

Wildfly Undertow 中的 HTTPS 重定向

使用 undertow-handlers.conf 重写路径无法按预期工作

如果不接受 cookie,配置 WildFly/Undertow 以将 JSESSIONID 放在 URL 上

Spring + Jboss7 @Transactional 不工作

Spring Cloud 升级之路 - 2020.0.x - 2. 使用 Undertow 作为我们的 Web 服务容器