关于Java:Jboss中的Spring Boot返回错误404

Spring boot return error 404 in Jboss

我有一个示例Spring Boot应用程序。它在Tomcat服务器中工作,但是当我产生战争并将其部署在jboss服务器(7.1.1)中时,我遇到404错误。

这是我的restController示例:

1
2
3
4
5
6
7
8
9
10
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!!";
   }
}

这是我的主要课程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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是这样的:

1
2
3
4
> <?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如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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>
    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>
        spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我使用以下网址运行应用程序:
http:// localhost:8080 / test / test,但返回404错误。

谢谢您的帮助。


我在JBoss EAP 6.4 / spring boot 1.5上遇到了相同的问题,并且解决了添加此属性的问题

1
server.servlet-path=/*

如本博文所述:在JBOSS EAP 6.1上部署spring boot

尽管在JBoss EAP 7.1上没有此属性,它也能正常工作。


我不确定您通过使用下面的属性到底要达到什么目的

1
server.contextPath = /*

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

时抛出以下错误

1
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

如果您尝试以

的身份访问网址

1
http://localhost:8080/test/test

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

1
server.contextPath = /test

否则,请勿将此属性添加到application.properties文件中,并且您可以访问

1
http://localhost:8080/test

在MainApp类上添加ComponentScan批注。
Spring组件扫描程序可能找不到您的@RestController批注

1
2
3
4
5
@SpringBootApplication
@ComponentScan(basePackages = {"com.blabla.*"})
public class MainApp extends SpringBootServletInitializer {
...
}

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