当前位置: 首页 > 知识库问答 >
问题:

Jboss7:逆流Spring Boot投掷404

徐丰茂
2023-03-14

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

我在为JBoss建立一个战争文件

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

但是当我给控制器打电话时,它会抛出一个带有嵌入式服务器的404,它工作正常。

砰.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://stackoverflow.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://stackoverflow.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>

类似于Wildfly和springboot的“404 - Not found”错误

共有1个答案

程振濂
2023-03-14

终于弄清楚我做了什么,我有一个带有main方法的Application类,没有@SpringBootApplication以及SpringBootServletOrializer,所以Jboss没有正确获取映射

解决方案是使Spring靴服务器初始化器类与主要方法。所以我改变了有问题的服务初始化器

@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生成项目< code>Application和< code > SpringBootServletInitializer 类时,我使用带有war打包的SPRING INITIALIZR生成了项目。在这两个类中,Appliaction类有main方法和< code > @ spring boot application 注释

虽然最初编写集成测试不起作用,但后来我将@SpringBootApplication注释更改为Spring引导服务器初始化器,并在Spring启动服务器初始化器测试中编写一个主要方法开始工作。但是我把主要方法保留在应用行动类中,这是这个问题的根本原因。

由于Spring初始化器,生成我在假设的类,所有生成的类都需要构建war。因此,在生成war文件时,我保留了< code>Application类的生成方式,没有使用< code > @ spring boot Application 注释。我认为Jboss搞不清楚应该运行哪个main方法。并最终在到达终点时抛出404异常

 类似资料:
  • 有人可以解释我什么时候使用关键字 throw new 有用.而不是在方法的签名旁边使用抛出? 我知道当方法抛出Checked Exception时。Java迫使我们通过将Exception处理到try-catch块中,或者通过在签名旁边的关键字throws指定将在其他地方完成,从而直接在方法中处理它。 然而,我很难理解何时使用关键字throw new是有用的,以及为什么。它与处理未选中的异常有关吗

  • 我正在用python运行下面的代码 我每次都会遇到下面的错误-有人能帮忙吗! OpenCV错误:未指定的错误(该函数未实现。使用Windows、GTK 2.x或Carbon支持重建库。如果您在Ubuntu或Debian上,请安装libgtk2.0-dev和pkg config,然后重新运行cmake或configure脚本),位于cvShowImage、file/Users/travis/buil

  • 我有两个类(SimManager和Sim),我想为它们创建模拟对象,然后使用存根方法。 测试在上面第9行抛出这两个错误跟踪。 第一: Java语言lang.VerifyError:org/mockito/cglib/core/reflectils at org。莫基托。cglib。果心KeyFactory$发电机。在org上生成Class(KeyFactory.java:167)。莫基托。cgli

  • 我在视图中有一个ImageView,并尝试用它显示一个WritableImage实例。我正在外线程中绘制它,并通过侦听ObjectProperty的更改事件将其传递给视图。 imageView应该准备好接收图像,它由我的主视图显示。但是这是一个不合法的例外 线程“Thread-4”Java . lang . illegalstateexception中的异常:不在FX应用程序线程上;当前线程=线程

  • 我有下面的代码,它按照预期工作: 我想知道是否有一种方法,只有一个orElseThrow-Part,或做它更少的冗余?

  • 我向服务器调用一个请求网络,服务器总是响应如下所示的格式json: 对于每种请求,只有jsonObject数据包含不同的字段。新响应的示例: 因此,我决定让一个通用类调用ServerResult包含一个字段“Object”,以便将其强制转换为特定对象。 //ServerResult.class //client.java 请求完成后,我通过GSON将json转换为ServerResult对象: 然