Get 404 error from Heroku when on localhost evetything ok. Java application

Viewed 49

I am trying to deploy my simple java application on Heroku. And I have successful status(in deploy) and it starts. But when I am trying to get access to my controllers by https://herokuprojectest.herokuapp.com/main/status, I get HTTP Status 404 – Not Found. And in heroku logs i have this message:

at=info method=GET path="/main/status" host=herokuprojectest.herokuapp.com request_id=42c4069e-ceef-425b-8786-8ab4834b1b7b fwd="188.163.51.90" dyno=web.1 connect=0ms service=3ms status=404 bytes=879 protocol=https

But when I am trying the same on http://localhost:8080/main/status, everything is ok, i get {"status":"ok"} - answer from controller.

My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> 
    </parent>
    <packaging>war</packaging>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.7.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>copy</goal></goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.heroku</groupId>
                                    <artifactId>webapp-runner</artifactId>
                                    <version>9.0.30.0</version>
                                    <destFileName>webapp-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

My Procfile: web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --port $PORT target/*.war

My Controller:

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/main")
public class MainController {

   @GetMapping("/status")
   public ResponseEntity<Model> getStatus(Model model, WebRequest request) {
       model.addAttribute("status", "ok");
       return ResponseEntity.ok(model);
   }

}

I think that I have problem in my pom or procfile, or I can be wrong… Does anyone see my mistake?

1 Answers

I am not familiar with this but:

  1. Try the next procfile: web: java -jar target/dependency/webapp-runner.jar target/*.war --port $PORT

  2. Don't forget to build the project before upload with: mvn package

  3. Are you sure about your $JAVA_OPTS var? In some cases developers should use different variations of it: _JAVA_OPTIONS, JAVA_TOOL_OPTIONS. If JAVA_OPTS is fine for your case, is it set up correctly?

I bet that your problem is in procfile.

Related