0

I have built an application with Spring Boot which I am deploying in a remote Tomcat server. Whenever I make a request to the only endpoint the application has I get a 404. I set all the context path to the same name /Tracker and still nothing. I don't know if I may be missing something.

This is the message I get:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

This is the URL I am sending the request: http://XXX.XXX.XX.XX:8081/Tracker/v01/project. The port is correct since I changed it.

I am using the same I use in my local environment which works perfectly.

Payload

{
"name" : "Test2",
"tag" : "TST"
}

Package structure

packages

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tlc</groupId>
<artifactId>tracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Tracker</name>
<description>Tracking Project</description>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.18</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <finalName>Tracker</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

</project>

application.yml

server:
 servlet:
   context-path: /Tracker
 port: ${PORT:8080}
spring:
jpa:
    hibernate:
        naming:
            physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: ${DATABASE_URL:jdbc:mysql://localhost:3306/test}
    username: ${DATABASE_USER:root}
    password: ${DATABASE_PASSWORD}
liquibase:
    enabled: true
    change-log: classpath:/db/liquibase/liquibase-changelog.xml

ProjectController

@RestController
@RequestMapping("/v01")
@Slf4j
public class ProjectController {

@Autowired
private ProjectServiceIface projectService;

@PostMapping(path = "/project", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Project> createProject(@Valid @RequestBody Project project, BindingResult result){
    if(result.hasErrors()){
        throw new BusinessServiceException(result.getFieldError().getDefaultMessage(), result.getFieldError().getField() + " " + result.getFieldError().getCode());
    }
    Project projectSaved = projectService.createProject(project);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Location", projectSaved.getId().toString());

    return new ResponseEntity<>(project, headers, HttpStatus.CREATED);
}
}
NeoChiri
  • 296
  • 4
  • 18
  • Can you post your controller code? What do you see in the Tomcat logs when the application starts? – Brian Ochs Jan 16 '20 at 18:28
  • I just pasted the controller's code. Logs show no errors. The application is deployed correctly. – NeoChiri Jan 16 '20 at 18:39
  • It sounds like Spring isn't scanning your controller class (ProjectController). Look here: https://stackoverflow.com/a/31318789/3135317 – FoggyDay Jan 16 '20 at 18:41
  • I tried that but still the same. Besides, my packaging name is correct according to what Spring Boot does by default when scanning them. – NeoChiri Jan 16 '20 at 18:45
  • How are you posting the request? What does the payload look like? – Brian Ochs Jan 16 '20 at 19:23
  • I just added it to the question. It is the same I use in my local environment which works perfectly. – NeoChiri Jan 16 '20 at 19:49
  • It seems like maybe the application is not deploying properly on the remote server. Is there anything at all in the Tomcat logs when you deploy/startup? – Brian Ochs Jan 16 '20 at 20:06
  • I just redeployed and even restarted tomcat and nothing. Logs are clean. I checked all the logs I found on `/opt/tomcat/logs`. If there is somewhere else to look, let me know, please. – NeoChiri Jan 16 '20 at 20:19

2 Answers2

1

I have found the issue and I had to extend the class SpringBootServletInitializer and override its method configure. This class must be used when you are deploying the application using a WAR file.

@SpringBootApplication
public class TrackerApplication extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(TrackerApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(TrackerApplication.class);
}

}
NeoChiri
  • 296
  • 4
  • 18
0

Try to change port like that:

port: 8080