无法在Tomcat*问已部署的WAR文件
我有一个在我的本地机器上运行的Spring Boot Rest服务项目。当我运行该应用程序为“春启动应用程序”无法在Tomcat*问已部署的WAR文件
我可以去访问其他服务
http://127.0.0.1:8080/persons/all
和它应该返回JSON。
我将pom.xml打包改成了war,然后我在Spring工具套装中以Run as - > Maven构建了一场战争。它创建一个战争文件。当我上传战争档案http://myserverip:8080/manager/我没有得到任何错误,它显示在应用程序
上传它后,战争文件名称是“myapp-0.0.1-SNAPSHOT.war”,我试图访问它
http://myserverip:8080/myapp-0.0.1-SNAPSHOT/persons/all
但我得到一个404错误
我在做什么错在这里?
我的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>com.mycompany</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>myappapi</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</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-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
当我构建应用程序,我得到以下日志
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myapp ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:2.6:war (default-war) @ myapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myapp] in [C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [586 msecs]
[INFO] Building war: C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.300 s
[INFO] Finished at: 2016-11-21T10:12:03-05:00
[INFO] Final Memory: 20M/243M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
我MyappApplication.java
package com.mycompany;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MyappApplication {
public static void main(String[] args) {
SpringApplication.run(MyappApplication.class, args);
}
}
延长SpringBootServletInitializer参见:Packaging executable jar and war files
正如评论中所提到的,传统上可以通过将war部署到独立的tomcat实例来部署spring引导应用程序。您需要对MyappApplication.java
课程进行一些更改,该课程有main()
方法。
-
让您
MyappApplication
延长SpringBootServletInitializer
并覆盖其configure()
方法,@SpringBootApplication public class MyappApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(MyappApplication.class, args); } }
下一个步骤是有包装的战争,而不是你已经有默认的罐子。
最后在您的
pom.xml
中将嵌入式tomcat容器的作用域更改为provided
,以便在部署到单独的实例时不会发生冲突。
如果要更改罐子春季开机大战增加以下依赖关系
<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>
清单改变弹簧启动可执行的JAR文件到一个可部署的战争。
更改包装的pom.xml到 “战争”,从 “罐子”
-
正如kuhajeyan提到,标志着嵌入式tomcat的依赖性,在pom.xml提供。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope>
-
让你的主弹簧引导应用类这样
@SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
您可以在pom.xml中将您的buid名称更改为myapp。检查服务器启动时是否注册了“/ persons/all”。如果没有,则会显示404错误,因为您的控制器未正确注册。 – Lucky
@Lucky,但应用程序在我的本地机器上正常工作。它有可能会在Spring本地注册,但在Tomcat上部署war文件时不能注册? – Arya
是的,请检查您的tomcat服务器日志。 Spring引导使用嵌入式tomcat。您正在部署到一个单独的tomcat实例中。两者都不同。 – Lucky