springboot本地启动,访问路径正常,外部tomcat运行,接口访问404

1、案例介绍:

  1.1结构:

   1.2 pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  
     .....................................................
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    1.3 Controller层代码:

@RestController
public class TestController {

    @GetMapping(value = "/")
    public String test1() {
        return "hello world";
    }


    @GetMapping(value = "/test")
    public Map<String, Object> test() {
        Map<String, Object> map = new HashMap<>();
        map.put("张三", 1);
        map.put("李四", 2);
        return map;
    }

}

1.4 spring boot的启动类

@SpringBootApplication
public class DebugDemoApplication{

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

1.5 简述

application.yaml中任何配置。

很简单的spring boot demo而已。在本地用直接run启动。完全ok。

并且浏览器访问localhost:8080(这里application.yaml并未配置server.servlet. context-path),可以在浏览器看到

2、问题

        就是这个简单的demo在本地用idea中,run启动,完全没有任何问题,但是使用maven打包成war包,然后使用tomcat启动,再去访问localhost:8080/项目名 (ps:这里跟springboot中用内置tomcat运行,访问路径不太一样)

       使用maven打完包后:demo-0.0.1-SNAPSHOT.war,将其改名demo.war丢到tomcat的webapps下。在bin目录中运行.status.bat运行tomcat。

访问浏览器localhost:8080/demo:

3.解决

    3.1 引入spring-boot-starter-tomcat

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
 </dependency>

   3.2 启动类

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 DebugDemoApplication extends SpringBootServletInitializer {

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

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

以上修改完毕后,使用idea再次run启动,访问浏览器localhost:8080:

再次打包、重命名、并启动tomcat服务器,查看放在外部tomcat服务器中的效果:

所以,综上问题已解决。

ps:

       本地springboot启动与tomcat服务器启动访问的接口的路径不太一致,这里解决也很简单,在spring boot的主配置文件application.yml中添加一段配置:

server:
  servlet:
    context-path: /demo   #项目名
发布了187 篇原创文章 · 获赞 146 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/qq_37495786/article/details/105016313
今日推荐