1

I have a Spring Boot App which runs when packaged as standalone jar with command:

java -jar myApp.jar

However, if I the packaging is war in pom.xml, I can't deploy to tomcat server. My app is a rest service which provides rest calls. When I run the app either by using eclipse or through java command, the app responds to rest calls, like:

http://www.spdev.ufu.br:8081/dashboard/rest/chart/getNumeroMetasPorUnidade

The rest provider is Jersey. Could anyone tell me why the war version does not work in tomcat ? I've done some previous research, but with no success:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

Spring Boot War deployed to Tomcat

Here 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>br.ufu.sp.dashboard</groupId>
<artifactId>sp-dashboard</artifactId>

<packaging>war</packaging>
<!-- <packaging>jar</packaging> -->

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.M3</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <start-class>br.ufu.sp.DashBoardApp</start-class>
</properties>


<dependencies>
    <dependency>
        <groupId>br.ufu.sp</groupId>
        <artifactId>sp-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <!-- specific project exclusions -->
        </exclusions>

    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</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-actuator</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-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

</dependencies>

<!-- Package as an executable jar -->
<build>
    <finalName>dashboard</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.4.RELEASE</version>
                </dependency>

            </dependencies>
        </plugin>
    </plugins>
</build>
</project>

This is my Jersey Configuration:

@Component
@ApplicationPath("/dashboard/rest")
public class JerseyConfig extends ResourceConfig {

public JerseyConfig() {

    packages("br.ufu.sp.dashboard.rest");
    packages("br.ufu.sp.dashboard.model");
    packages("br.ufu.sp.dashboard.repository");
    packages("br.ufu.sp.dashboard.service");

  }

}

and my app configuration:

@SpringBootApplication
@EnableTransactionManagement
public class DashBoardApp extends SpringBootServletInitializer {

public static void main(String[] args) {
    new DashBoardApp().configure(new SpringApplicationBuilder(DashBoardApp.class)).run(args);
}


@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DashBoardApp.class);
}

}

Update 1: I checked tomcat's log and found out that the context could not be initialized. Here follows the message:

INFO: Spring WebApplicationInitializers detected on classpath: [br.ufu.sp.DashBoardApp@5a8d3b61, org.glassfish.jersey.server.spring.SpringWebApplicationInitializer@5ca8599b, org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@79d5f5ac] set 15, 2015 10:36:47 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring embedded WebApplicationContext set 15, 2015 10:36:53 AM org.apache.catalina.core.StandardContext listenerStart 
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml! at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:299)...

Update 2: According to

https://java.net/jira/si/jira.issueviews:issue-html/JERSEY-2038/JERSEY-2038.html

A class was needed to set the context issue. Anyway, this is not the problem now. Somehow my endpoints are not being loaded, as shows the tomcat log:

[DEBUG] 2015-09-15 11:14:30,278 org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping getHandlerInternal - Looking up handler method for path /rest/chart/getNumeroMetasPorUnidade
[DEBUG] 2015-09-15 11:14:30,284 org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping getHandlerInternal - Did not find handler method for [/rest/chart/getNumeroMetasPorUnidade]
Community
  • 1
  • 1
Digao
  • 520
  • 8
  • 22

0 Answers0