【问题标题】:Spring boot return error 404 in JbossJboss中的Spring Boot返回错误404
【发布时间】:2018-08-23 07:13:44
【问题描述】:

我有一个示例 Spring Boot 应用程序。它可以在 Tomcat 服务器中运行,但是当我生成战争并将其部署在 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!!";
   }
}

这是我的主要课程:

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>

我使用这个 url 运行应用程序: http://localhost:8080/test/test 但返回了 404 错误。

感谢您的帮助。

【问题讨论】:

    标签: java spring spring-boot jboss


    【解决方案1】:

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

    server.servlet-path=/*
    

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

    不过,在 JBoss EAP 7.1 上没有这个属性也能正常工作。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        在您的 MainApp 类上添加 ComponentScan 注释。 可能你的@RestController 注解没有被spring组件扫描器找到

        @SpringBootApplication
        @ComponentScan(basePackages = {"com.blabla.*"})
        public class MainApp extends SpringBootServletInitializer {
        ...
        }
        

        【讨论】:

          【解决方案4】:

          我不确定您究竟想通过使用以下属性来实现什么

          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 
          

          如果您尝试访问网址

          http://localhost:8080/test/test 
          

          然后,更新属性文件如下

          server.contextPath = /test
          

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

          http://localhost:8080/test 
          

          【讨论】:

            猜你喜欢
            • 2018-07-15
            • 1970-01-01
            • 2020-11-13
            • 1970-01-01
            • 1970-01-01
            • 2020-10-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多