簡體   English   中英

Heroku Spring Boot Web服務始終返回404但不在localhost上

[英]Heroku Spring Boot web service always return 404 but not on localhost

我開發了一個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啟動我的應用程序時,注釋映射不會被“編譯”,因為它沒有打印在日志中,但我只是在猜測。

我的Procfile:

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文檔以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM