已部署 Spring-启动 war 在 Tomcat 中不起作用
Deployed Spring-Boot war doesn't work in Tomcat
我使用 Java8 将我的 Spring-Boot 应用程序部署到 Tomcat 8.5 中,但是当我使用 CURL 发送一些请求时,我收到 404 响应。我不知道我做错了什么。
我对 Java 并不陌生,但这是我第一次在服务器上设置环境来部署我的应用程序。我关注的博客post:https://linuxize.com/post/how-to-install-tomcat-9-on-ubuntu-18-04/
唯一的区别是我在 Ubuntu 16.04 上安装了 Tomcat 8.5 并且我没有设置管理器界面。
我的Spring-引导入口点:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
我的pom.xml:
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<groupId>
some.group.id
</groupId>
<artifactId>
server
</artifactId>
<!--<version>-->
<!--1.0-SNAPSHOT-->
<!--</version>-->
<properties>
<commons-lang.version>3.6</commons-lang.version>
<guava.version>25.0-jre</guava.version>
<jwt.version>0.9.0</jwt.version>
<joda-time.version>2.9.9</joda-time.version>
<junit.version>4.12</junit.version>
<mockito.version>2.17.0</mockito.version>
<start-class>some.group.id.Application</start-class>
</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>${guava.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.8.1</version>
</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>
我开始 Tomcat 作为一项服务:
Description=Tomcat 8 servlet container
After=network.target
[Service]
Type=forking
User=tomcatuser
Group=tomcatuser
#Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
JAVA_HOME设置在/etc/environment中,所以被注释掉了。
我将我的应用程序部署到 AWS,它可以正常工作。当我从 IntelliJ 在本地 运行 它时,它也可以工作。所以,我认为这与我的 Tomcat 设置有关。另外,我没有任何日志文件,例如 catalina.out.
@lealceldeiro 的回答对我有帮助
错误是我请求的资源没有应用程序上下文路由的名称。我部署的 war 被命名为 "app.war",因此,我请求的 URL 是错误的:
host-address:8080/resource-uri
右边的URL是:
host-address:8080/app/resource-uri
我使用 Java8 将我的 Spring-Boot 应用程序部署到 Tomcat 8.5 中,但是当我使用 CURL 发送一些请求时,我收到 404 响应。我不知道我做错了什么。
我对 Java 并不陌生,但这是我第一次在服务器上设置环境来部署我的应用程序。我关注的博客post:https://linuxize.com/post/how-to-install-tomcat-9-on-ubuntu-18-04/
唯一的区别是我在 Ubuntu 16.04 上安装了 Tomcat 8.5 并且我没有设置管理器界面。
我的Spring-引导入口点:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
我的pom.xml:
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<groupId>
some.group.id
</groupId>
<artifactId>
server
</artifactId>
<!--<version>-->
<!--1.0-SNAPSHOT-->
<!--</version>-->
<properties>
<commons-lang.version>3.6</commons-lang.version>
<guava.version>25.0-jre</guava.version>
<jwt.version>0.9.0</jwt.version>
<joda-time.version>2.9.9</joda-time.version>
<junit.version>4.12</junit.version>
<mockito.version>2.17.0</mockito.version>
<start-class>some.group.id.Application</start-class>
</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>${guava.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.8.1</version>
</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>
我开始 Tomcat 作为一项服务:
Description=Tomcat 8 servlet container
After=network.target
[Service]
Type=forking
User=tomcatuser
Group=tomcatuser
#Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
JAVA_HOME设置在/etc/environment中,所以被注释掉了。
我将我的应用程序部署到 AWS,它可以正常工作。当我从 IntelliJ 在本地 运行 它时,它也可以工作。所以,我认为这与我的 Tomcat 设置有关。另外,我没有任何日志文件,例如 catalina.out.
@lealceldeiro 的回答对我有帮助
错误是我请求的资源没有应用程序上下文路由的名称。我部署的 war 被命名为 "app.war",因此,我请求的 URL 是错误的:
host-address:8080/resource-uri
右边的URL是:
host-address:8080/app/resource-uri