• springboot 使用tomcat 启动 404 问题解决方式


    学习备忘,希望对你有所帮助!!!

    前言(废话)

    为啥要用tomcat 启动spring boot 项目呢,spring boot 不是自带tomcat 吗? 原因本人嫌弃springboot 的热部署太慢了,纯属个人习惯。

    1、先做一个SpringBoot 项目,过程不赘述,网上资源太多了。这里只给出pom文件的核心部分。

         1)打包方式,配置tomcat 时会出来一个war 让你选择。

    <packaging>war</packaging>

         2)添加web 支持,加了这个依赖就可以使用SpringMvc了,写controller 的包都在这里。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>    

    2、写一个controller 供测试

    package com.chaoming.demo.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class TestController {
    @RequestMapping("/test")
    public String test(){
    return "Success";
    }
    }

    3、SpringBoot 模式启动

         1)访问:http://localhost:8080/test

         2)返回结果:

            

     4、配置Tomcat启动,访问上述地址报 404错误。

           

     5、解决方式

          修改启动类,让类继承 SpringBootServletInitializer,重写 configure 方法

    package com.chaoming.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class DemoApplication extends SpringBootServletInitializer {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(DemoApplication.class);
        }
    }

    6、验证

     至此两种方式启动都可以正常访问。

    学习备忘,好记性的烂笔头。

  • 相关阅读:
    win_tc使用感受
    10进制转8进制(栈操作)
    动态栈
    数组
    单链表学习
    static用法
    基础2
    linux c first
    linux net command /uboot command
    opencv
  • 原文地址:https://www.cnblogs.com/spqin/p/13358071.html
Copyright © 2020-2023  润新知