Faabass
Faabass

Reputation: 1164

Java app working when running spring boot but not with Apache

I created an app using https://start.spring.io/ so when I run ./mvnw spring-boot:run the apps work great. But then in order to be able to debug my app (as I always did with others) I select "Debug On Server" and the server compile successfully with the message INFORMACIÓN: Server startup in 4288 ms

But when I try to access to the app I get 404 error.

This is my 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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.domain.app</groupId>
    <artifactId>app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>app</name>
    <description>app</description>

    <properties>
        <java.version>1.8</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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
        </dependency>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>
    </dependencies>

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

</project>

Thhis is my controller:

@RestController
@RequestMapping(
        value = "/api/products",
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE
)
public class ProductController {

    @Autowired
    ProductService productService;

    @GetMapping("")
    @ResponseStatus(HttpStatus.OK)
    public String getAllProducts() {
        return this.productService.initCrawler();
    }
}

And the URL I'm pointing is:

http://localhost:8080/app/api/products

And the response is HTTP 404

Also eclipse after the build open the explorer in http://localhost:8080/app but it gets also 404.

When running directly with ./mvnw spring-boot:run the URL that works fine is http://localhost:8080/api/products which is not working neither when I use Run as Server

And in the logs doesn't say anything...

Upvotes: 1

Views: 1480

Answers (1)

Rando Shtishi
Rando Shtishi

Reputation: 1482

I understood your question this way. You have created a spring boot application from Spring Initializr and the application run successfully from command line but not when you are trying to deployed in server and start the server in debug mode.

You should know that Spring Initializr created a spring boot application which is stand-alone, production-grade Spring based Applications that you can "just run". With stand-alone meaning the the server is incorporated as dependency in spring boot app and you don't need to deployed in server.

If you want to deploy in server , you need to make Spring Boot Application @SpringBootApplication class extend the SpringBootServletInitializer class.


@SpringBootApplication
public class DemoApplication  extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(DemoApplication.class);
   }
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

You should specify in maven the main class that is going to run and change the packaging from jar to war.

<packaging>war</packaging>
<start-class>com.tutorialspoint.demo.DemoApplication</start-class>

For more information how to make you spring boot application to be deployed in server, check the link : https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm

If you don't need the application to run on the server , you just want to debug the code. I would suggest to use eclipse or intellij. You include spring boot dashboard in perspective and start the spring boot application in debug mode. Check this link to understand how to include and use spring boot dashboard : https://medium.com/danielpadua/java-spring-boot-eclipse-744454468670

Upvotes: 1

Related Questions