我有一个Spring Boot应用程序,当使用命令打包为独立jar时运行:
java -jar myApp.jar
但是,如果我的包装在pom.xml中是war,我就无法部署到tomcat服务器。我的应用程序是休息服务,提供休息呼叫。当我通过使用eclipse或通过java命令运行应用程序时,应用程序会响应其余的调用,例如:
http://www.spdev.ufu.br:8081/dashboard/rest/chart/getNumeroMetasPorUnidade
其他提供者是泽西岛。谁能告诉我为什么战争版本在tomcat中不起作用?我之前做了一些研究,但没有成功:
Spring Boot War deployed to Tomcat
这是我的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>
这是我的泽西配置:
@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");
}
}
和我的应用配置:
@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);
}
}
更新1: 我检查了tomcat的日志,发现上下文无法初始化。以下是消息:
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)...
更新2: 根据
https://java.net/jira/si/jira.issueviews:issue-html/JERSEY-2038/JERSEY-2038.html
需要一个类来设置上下文问题。无论如何,这不是现在的问题。不知怎的,我的端点没有被加载,如tomcat日志所示:
[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]