【问题标题】:deploy spring boot war into tomcat将 Spring Boot War 部署到 tomcat 中
【发布时间】:2018-05-18 13:22:21
【问题描述】:

我正在使用 spring boot,我想将一个 rest API war 文件部署到 tomcat 服务器中。 tomcat 服务器日志中没有错误,但是当我调用任何端点时,我得到 404“未找到”,并且我从 tomcat 服务器得到了相同的结果。

java 版本:8。 tomcat 版本:9.

也许我错过了什么,这是我的代码示例:

波姆:

<?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.test</groupId>
<artifactId>test</artifactId>
<version>1.1</version>
<packaging>war</packaging>

<name>test</name>
<description>test API</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.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>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

        <!-- tag::security[] -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- end::security[] -->


    <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-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>

<build>
    <defaultGoal>install</defaultGoal>

    <pluginManagement>
        <plugins>
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>

        </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
 </build>
 </project>

2.Application.java

@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {

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


public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}

}

【问题讨论】:

  • 如果您使用的是spring-boot,那么您可以使用嵌入式tomcat。您不需要将它部署在另一个 tomcat 服务器上,而是将其打包到 jar &lt;packaging&gt;jar&lt;/packaging&gt; 并在构建后使用命令运行您的应用程序 java -jar abc.jar
  • 在我的例子中,我从webapps 中删除了ROOT 目录,因为我没有使用根上下文。我只使用像www.example.com/myAppName 这样的完整URL。我仍然开始得到 404。它在放回 ROOT 目录后工作。试图找出原因......

标签: spring tomcat spring-boot


【解决方案1】:

是的,我遇到了类似的错误,在我的情况下,我自动装配 Spring 安全会话的对象,因此它显示循环引用错误。 我在自动装配的对象上添加了@Lay 注释。有关更多详细信息,请检查链接。

https://www.quora.com/How-can-you-fix-Requested-bean-is-currently-in-creation-Is-there-an-unresolvable-circular-reference-while-deploying-a-Spring-Security-application-in-Tomcat

【讨论】:

    【解决方案2】:

    在您的 tomcat 服务器中作为 ROOT 上下文运行您的 Spring Boot 应用程序。

    你可以通过使用你的 spring boot 应用程序更改 tomcat 服务器中的 ROOT.war 来做到这一点(这是一个不好的方法),或者你可以配置它。

    【讨论】:

    【解决方案3】:

    我修复了类似的问题。尝试打开网页时没有错误,但有一些警告。可能您可以采取“警告:未找到映射”。 您不需要 SpringBootServletInitializer 并覆盖应用程序类中的metode。 Spring Boot 是为自动配置而开发的。您的 html 或 jsp 页面应位于 resources/template 或 resources/static 文件夹中。请仔细阅读Serving Web Content with Spring MVCthis 页面可以帮助你。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2018-12-05
      • 2020-06-17
      • 1970-01-01
      • 2016-08-11
      • 2014-09-04
      • 2016-02-15
      • 1970-01-01
      • 2023-01-03
      相关资源
      最近更新 更多