springboot项目打成war包部署到外部tomcat上,排除依赖出现问题

springboot项目打成war包部署到外部tomcat上,排除依赖出现问题

问题描述:

pom文件build模块:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 没有web.xml 配置的话, 配置为false-->
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <packagingExcludes>
<!--                        排除依赖包-->
                        WEB-INF/lib/*.jar
                    </packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

<!--依赖分离-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!--<outputDirectory>target/lib</outputDirectory>-->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如果带着依赖打包,接口可以正常访问

但把依赖包分离出来就无法访问接口, 但是服务能启动

注释内容:

                    <packagingExcludes>
<!--                        排除依赖包-->
                        WEB-INF/lib/*.jar
                    </packagingExcludes>

img

项目是启动成功的
但是无法访问接口
对比了一下有依赖的启动日志
发现去除依赖后少了以下两行日志,怀疑是'dispatcherServlet'没有映射接口路径导致, 有遇到过的嘛

img

那是因为你访问了controller,才会初始化dispatchServlet,十有八九是你访问接口url错了