springboot打成war包部署到tomcat

官方链接:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

关于打war包让我们看看官网怎么说的

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container. Typically, you update your application’s main class to extend SpringBootServletInitializer:

一 、修改启动类,记得将启动类继承 SpringBootServletInitializer 并且重写 configure方法即可

@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);
}

}

二、修改pom文件的打包方式

war

三、添加打包插件:finalName 可以修改打包的打成war包的名称


        websocket
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        

如果你的插件中有 : 要按照以下方式注释掉。


    org.springframework.boot
    spring-boot-maven-plugin
    
         com.BlogWebApplication
        jar
               true
    -->

四、在Pom.xml文件中添加以下依赖


        org.springframework.boot
        spring-boot-starter-tomcat
        provided

或者这样子也可以


            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
  

五、使用clean先清理下,防止旧文件影响,然后执行package打包命令,即可打成war包

可能遇到的问题:一部分错误代码

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webServerFactory' defined in class path resource [com/blog/framework/config/TomcatConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory]: Factory method 'webServerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/coyote/UpgradeProtocol

at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$139/1797087821.getObject(Unknown Source)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)

一月 17, 2019 123 下午 org.apache.catalina.core.ContainerBase addChildInternal
严重: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/blog]]
解决方法,更换成8或者8.5以上的tomcat即可,linux服务器也一样,使用高版本的tomcat就行,我的使用的时8.5可以正常启动

如果你还有其他问题,你可以查看 https://blog.csdn.net/weixin_38312502/article/details/81026961

这上面也有一些常见错误。

标签: SpringBoot

仅有一条评论

  1. 抉择 2019年07月13日 09:49

    很有用!

    回复

添加新评论