Heroku Spring Boot Web服务始终返回404,但不在本地主机上 - java

我已经开发了一个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

我希望有一个人可以帮助我。

参考方案

spring-boot-starter-parent POM包含<executions>配置以绑定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以获取更多信息。

无法将应用程序日志保存到文件 - java

我正在创建一个简单的Spring Boot应用程序,该应用程序会在API调用上产生结果。该应用程序按预期工作,但我无法将日志打印到文件中。日志仅在控制台中显示。这是我的pom文件:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="h…

Spring Boot索引未加载 - java

我有一个Spring Boot应用程序。我没有使用@EnableWebMvc,并且我的资源在src/main/resources/static文件夹中。当我尝试加载localhost:8080/ui/时,它只是下载一个随机文件(类型:八位字节流)。如果我直接使用/ui/index.html则可以。我也在使用WebSecurityConfigurerAdapt…

Spring MVC拦截器映射问题 - java

我有这段XML:<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/statics/**" /> <bean class="com.company.website.servlet.StaticsHandlerIntercept…

在Maven中创建模块 - java

我创建了这个maven子项目,它将包含项目的域对象: <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-start…

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…