无法访问 Tomcat 上部署的 WAR 文件
Can not access deployed WAR file on Tomcat
我有一个 Spring 启动休息服务项目,可以在我的本地机器上运行。当我运行申请时"Spring Boot App"
我可以通过
访问其余服务
http://127.0.0.1:8080/persons/all
并且它 returns JSON 正如它应该的那样。
我将 pom.xml 包装更改为 war,然后我通过转到 运行 创建了一个 war as -> Maven build in Spring tools套装。它创建一个 war 文件。当我在 http://myserverip:8080/manager/ 上上传 war 文件时,我没有收到任何错误,它显示在应用程序
下
上传后war文件名为"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);
}
}
如果您要将 jar 更改为 spring 引导的 war 添加以下依赖项
<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>
将 spring 启动可执行 jar 更改为可部署 war 的检查清单。
将 pom.xml 中的包装从 "jar"
更改为 "war"
如 kuhajeyan 所述,标记 tomcat 中提供的嵌入式依赖项 pom.xml。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
让你的主要 spring 引导应用程序 class 像这样扩展 SpringBootServletInitializer
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
参考:Packaging executable jar and war files
如评论中所述,您可以通过将 war 部署到独立的 tomcat 实例来传统地部署 spring 启动应用程序。您需要对具有 main()
方法的 MyappApplication.java
class 进行一些更改。
使您的 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);
}
}
下一步是将包装为 war 而不是您已有的默认 jar。
最后在您的 pom.xml
中将嵌入式 tomcat 容器的范围更改为 provided
,这样当您部署到单独的实例时它就不会发生冲突。
我有一个 Spring 启动休息服务项目,可以在我的本地机器上运行。当我运行申请时"Spring Boot App"
我可以通过
访问其余服务http://127.0.0.1:8080/persons/all
并且它 returns JSON 正如它应该的那样。
我将 pom.xml 包装更改为 war,然后我通过转到 运行 创建了一个 war as -> Maven build in Spring tools套装。它创建一个 war 文件。当我在 http://myserverip:8080/manager/ 上上传 war 文件时,我没有收到任何错误,它显示在应用程序
下上传后war文件名为"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);
}
}
如果您要将 jar 更改为 spring 引导的 war 添加以下依赖项
<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>
将 spring 启动可执行 jar 更改为可部署 war 的检查清单。
将 pom.xml 中的包装从 "jar"
更改为 "war"
如 kuhajeyan 所述,标记 tomcat 中提供的嵌入式依赖项 pom.xml。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope>
让你的主要 spring 引导应用程序 class 像这样扩展 SpringBootServletInitializer
@SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
参考:Packaging executable jar and war files
如评论中所述,您可以通过将 war 部署到独立的 tomcat 实例来传统地部署 spring 启动应用程序。您需要对具有 main()
方法的 MyappApplication.java
class 进行一些更改。
使您的
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); } }
下一步是将包装为 war 而不是您已有的默认 jar。
最后在您的
pom.xml
中将嵌入式 tomcat 容器的范围更改为provided
,这样当您部署到单独的实例时它就不会发生冲突。