【问题标题】:Heroku Spring Boot web service always return 404 but not on localhostHeroku Spring Boot Web 服务总是返回 404 但不在本地主机上
【发布时间】:2016-09-20 21:24:32
【问题描述】:

我开发了一个 Web 服务,我正在尝试将它部署到 Heroku。尽管每当我尝试调用其中一个 URL 时,它似乎成功了,但我得到一个 404。我知道这意味着该资源不可用,但它在 localhost 上工作正常。

Heroku 部署的输出是标准 Spring 引导,但缺少:

2016-05-23 22:29:22.145  INFO 15785 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/home],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<entity.Giver>> controller.GiverController.getHomeContent(javax.servlet.http.HttpServletRequest)

我通常通过在 localhost 上运行应用程序获得的行(输出只是我拥有的众多行之一)。

我怀疑当 Heroku 启动我的应用程序时,注释映射没有得到“编译”,因为它没有打印在日志中,但我只是猜测。

我的档案:

web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/hams-x-jar-with-dependencies.jar

这是我的 pom.xml 文件中的依赖项和插件(我排除了打包等,因为它们不应该很重要):

<properties>
    <java.version>1.8</java.version>
    <spring.boot.version>1.3.3.RELEASE</spring.boot.version> <!--Variable-->
    <tomcat.version>8.0.32</tomcat.version>
</properties>

<!--
Allows versioning and not to use the parent tag for spring boot framework
-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1207</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate5</artifactId>
        <version>2.7.4</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
        </plugin>

        <plugin>
            <groupId>com.heroku.sdk</groupId>
            <artifactId>heroku-maven-plugin</artifactId>
            <version>1.0.0</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>controller.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>controller.Application</mainClass>
                    </manifest>
                </archive>

            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

控制器使用@RestController 以及路径的@RequestMapping 注解:

@RestController
public class GiverController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ResponseEntity<List<Giver>> getHomeContent(HttpServletRequest request) {
        List<Giver> homepageContent = GiverAPI.getHomePageContent();

        if (homepageContent == null) {
            return new ResponseEntity<List<Giver>>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<List<Giver>>(homepageContent, HttpStatus.OK);
    }

}

该应用程序是一个 Maven 项目,其中包含一个带有 Spring Boot 的 TomCat servlet,它将所有内容打包到一个 .jar 文件中。

我用于部署的 Heroku CLI 命令是:

mvn clean heroku:deploy

我希望有人可以帮助我。

【问题讨论】:

  • 在你的本地机器上做一个mvn clean package 并尝试使用java -jar target/hams-x-jar-with-dependencies.jar 运行打包的jar,然后看看会发生什么

标签: java spring maven heroku spring-boot


【解决方案1】:

spring-boot-starter-parent POM 包含&lt;executions&gt; 配置以绑定repackage 目标。如果您不使用父 POM,则需要自己声明此配置。因此,删除 maven-assembly-pluginmaven-jar-plugin 插件定义并使用以下内容:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.version}</version>
    <configuration>
        <mainClass>controller.Application</mainClass>
        <layout>ZIP</layout>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

同时更改Procfile 并在那里使用新的jar 文件名。当您不使用父 pom 时,这是创建可执行 jar 的最简单方法。查看Spring Boot documentation 了解更多信息。

【讨论】:

  • 非常感谢阿里!
猜你喜欢
  • 2021-07-15
  • 2011-07-25
  • 2018-07-15
  • 2020-07-29
  • 2021-02-10
  • 2020-10-07
  • 2020-03-02
  • 1970-01-01
  • 2017-02-05
相关资源
最近更新 更多