淘先锋技术网

首页 1 2 3 4 5 6 7

因为springboot嵌入了tomcat,且默认是打jar包,所以直接打war包会有很多错误,今天就分享下springBoot项目将默认打jar包方式改成war包。

环境:jdk1.8,tomcat8.5

1、将pom文件中的packaging由jar改为war

<packaging>war</packaging>

2、移除内置tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 移除内嵌tomcat -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3、添加servlet-api依赖

<!-- servlet的api依赖 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

4、修改启动类继承SpringBootServletInitializer,并重写configure方法

@SpringBootApplication
@ServletComponentScan
public class TestApplication extends SpringBootServletInitializer {

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

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

5、如果有过滤器的话,需要重写过滤器的init和destroy方法

@WebFilter(filterName = "TestFilter",urlPatterns = "/*")
public class TestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse rsp = (HttpServletResponse) response;
        chain.doFilter(req,rsp);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}

如果不重写init和destroy的话会报如下错误

catalina.2019-08-21.log

严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal One or more Filters failed to start. Full details will be found in the appropriate container log file
严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [***] startup failed due to previous errors
这个日志里告诉我们过滤器有问题,导致启动失败,并让我们在其他的日志文件查看详细的报错

下面来看详细的报错
localhost.2019-08-21.log
Exception starting filter [TestFilter]
java.lang.AbstractMethodError
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4637)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5282)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
at org.apache.catalina.startup.HostConfig D e p l o y W a r . r u n ( H o s t C o n f i g . j a v a : 1857 ) a t j a v a . u t i l . c o n c u r r e n t . E x e c u t o r s DeployWar.run(HostConfig.java:1857) at java.util.concurrent.Executors DeployWar.run(HostConfig.java:1857)atjava.util.concurrent.ExecutorsRunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

这里告诉我们在初始化的时候就报错(因为是先执行init所以这里没看到destroy的报错)

参考文档:

1、https://blog.csdn.net/weixin_38312502/article/details/81026961

2、https://blog.csdn.net/xiaofo258/article/details/93975934