Spring 在 Jboss 中启动 return 错误 404

Spring boot return error 404 in Jboss

我有一个示例 Spring 启动应用程序。它在 Tomcat 服务器中工作,但是当我生成 war 并将其部署在 jboss 服务器(7.1.1)中时,出现 404 错误。

这是我的 restController 示例:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
   @RequestMapping(value="/test")
   public String sayHello() {
      return "Hello Spring Boot!!";
   }
}

这是我的主要 class :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class MainApp extends SpringBootServletInitializer {
   public static void main(String[] args) {
      SpringApplication.run(MainApp.class, args);
   }
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
   return application.sources(MainApp.class);
   }
}

我添加了一个 application.properties 文件,并在其中添加了这一行:

server.contextPath = /*

我的jbos-web.xml是这样的:

> <?xml version="1.0" encoding="UTF-8"?> <!--   this is really not
> needed...   you can just build (or rename WAR file to)
> spring-boot-jboss.war
> --> <!DOCTYPE jboss-web> <jboss-web>   <context-root>/test</context-root> </jboss-web>

最后我的 pom.xml 如下:

<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.boraji.tutorial.springboot</groupId>
    <artifactId>spring-boot-hello-world-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <java.version>1.7</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <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>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

i 运行 使用此 url 的应用程序: http://localhost:8080/test/test 但返回了 404 错误。

感谢您的帮助。

您的 jboss-web.xml 应该位于 scr/main/webapp/WEB-INF/ 然后 Jboss 将在开始时使用它并使用您定义的 context-root.

在您的 MainApp 上添加 ComponentScan 注释 class。 spring 组件扫描器

可能找不到您的 @RestController 注释
@SpringBootApplication
@ComponentScan(basePackages = {"com.blabla.*"})
public class MainApp extends SpringBootServletInitializer {
...
}

我不确定你到底想通过下面的方法实现什么 属性

server.contextPath = /*

但是,如果您想为您的应用程序提供根上下文路径,那么它必须是某个字符串值而不是 astrike(代表一种模式)。而且,实际上这是不允许的。如果我在使用 tomcat 服务器时使用相同的 属性。 Tomcat 注册 Web 模块时抛出以下错误

2018-08-23 21:26:58.500 ERROR 13612 --- [ost-startStop-1] org.apache.tomcat.util.modeler.Registry  : Error registering Tomcat:j2eeType=WebModule,name=//localhost/*,J2EEApplication=none,J2EEServer=none 

如果您尝试访问 url 作为

http://localhost:8080/test/test 

然后,使用以下内容更新 属性 文件

server.contextPath = /test

否则,不要在application.properties文件中添加这个属性,您可以访问

http://localhost:8080/test 

我在 JBoss EAP 6.4 / spring boot 1.5 上遇到了同样的问题,解决它的方法是添加这个 属性

server.servlet-path=/*

如本 post 中所述:Deploying spring boot on JBOSS EAP 6.1

尽管在 JBoss EAP 7.1 上没有这个 属性 也能正常工作。