404 Not found Spring boot war to Tomcat [英] 404 Not found Spring boot war to Tomcat

查看:47
本文介绍了404 Not found Spring boot war to Tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Spring Boot 应用程序.这适用于嵌入式服务器.我可以使用 Postman 来测试 API.

I have Spring boot applicatio. This works fine with embedded server. And i can use Postman to test API.

现在我想在 Tomcat 8 上部署它,我用 maven install 创建 root.war 文件并将其放入 Tomcat/webapps/我运行tomcat没有任何错误.但是当我使用与邮递员相同的网址时,它给我 404 未找到.

Now i want deploy this on Tomcat 8 i create root.war file with maven install and put it to Tomcat/webapps/ I run tomcat without any errors. But when i use the same url as postman it gives me 404 not found.

我用于两者(嵌入式和 tomcat)的 url 示例:localhost:8080/auth/registration

Example of url i use for both(embedded and tomcat): localhost:8080/auth/registration

我尝试在 application.properties 中设置 server.context-path 但没有结果.

I've tried to set server.context-path in application.properties but no result.

我认为问题出在项目的配置上.

I believe that problem is in configuration of project.

这是我的 pom.xml

Here is my pom.xml

parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>

<dependencies>
    <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>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-core</artifactId>
        <version>1.14</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>

    <!--Dependency for singleton-->
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>javax.ejb-api</artifactId>
        <version>3.2</version>
    </dependency>


    <!--PostgreSQL driver-->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>


    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>

    <!--SEND MAIL-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>7.0.21</version>
        <scope>provided</scope>
    </dependency>

    <!--Amazon-->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.331</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>





<build>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </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>
    <finalName>ROOT</finalName>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

已编辑

此外,我发现 Spring Boot 可以比 war 更好的方式编译为 JAR.如果在 jar 中编译时如何更改 pom.xml 更好.

Also i have found that spring boot can be compiled to JAR as better way than war. If it's better how to change pom.xml for compiling in jar.

所以我尝试按照下面的建议创建 jar.我用 java -jar 运行它并得到org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class] 中定义的名称为tomcatEmbeddedServletContainerFactory"的 bean 创建时出错:bean 初始化失败;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration' 的 bean 时出错:通过构造函数参数 0 表示的不满足的依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties"的 bean 时出错:bean 初始化失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException

So i've tried to create jar as suggested below. I run this with java -jar and got org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException

     Caused by: java.lang.ClassNotFoundException: javax.xml.bind.ValidationException
at java.base/java.net.URLClassLoader.findClass(Unknown Source) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ~[na:na]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[rehapp-api.jar:1.0-SNAPSHOT]
at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ~[na:na]
... 83 common frames omitted

推荐答案

Step1:

pom.xml 添加/更新以下依赖项(范围为 provided),以告诉 Spring Boot 不要使用其嵌入式 Tomcat.

Step1:

Add/Update the following dependency to pom.xml (scope as provided) in order to tell Spring Boot not to use its embedded Tomcat.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

步骤 2:

<properties>
      <packaging>war</packaging>
</properties>

步骤 3:

SpringBootApplication 类应如下所示:

Step3:

SpringBootApplication class should look like below:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

参考:创建可部署的战争文件Maven 打包

这篇关于404 Not found Spring boot war to Tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆