springboot打包成war,部署到tomcat,访问404

一、打成war包发布到tomcat(这步已经完成,自行跳过)

1. pom.xml

去掉内嵌tomcat

`
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope><!-- 打成war的时候打开注释,本地开发用内嵌tomcat时加上注释 -->
    </dependency>
`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

打成war

`
    <packaging>war</packaging>
`
  • 1
  • 2
  • 3
  • 4

2. 启动类

`
    @SpringBootApplication
    @ComponentScan(basePackages = "com.yzker")
    public class Application extends SpringBootServletInitializer {

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

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


    }

`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

mvn clean package 将war包放到tomcat下试试吧,如果报404,参考下面 
访问地址:localhost:{tomcat端口号} /重命名的war包名/xxxx


二、404解决办法

原因后续补充

1. pom.xml

`
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-legacy</artifactId>
        <version>1.1.0.RELEASE</version>
    </dependency>
`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 修改web.xml

复制下面的代码,修改com.yzker.test.application.Application为你自己的启动类

`
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.yzker.test.application.Application</param-value>
      </context-param>

      <listener>
        <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
      </listener>

      <!--
          <filter>
              <filter-name>metricFilter</filter-name>
              <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
          </filter>

          <filter-mapping>
              <filter-name>metricFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
      -->

      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextAttribute</param-name>
          <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

</web-app>
`
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

3. 到此为止,404的问题解决,如果还有问题,请评论,我们共同解决记录

mvn clean package 
将target目录下生成war包放到tomcat里吧 
访问地址:localhost:{tomcat端口号} /重命名的war包名/xxxx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值