SpringMvc项目改造成springboot+web项目踩坑指南

SpringMvc项目改造成springboot+web项目踩坑指南

此前有个项目采用的springMvc框架实现的,每次启动都要依赖tomcat。
觉得比较麻烦,就准备改成springboot+web的方式。
磕磕碰碰,遇到了几个问题,记录过程如下:

项目改动前后结构对比

改动内容如下

  1. 新增StartupApplication.java作为启动类
    1
    2
    3
    4
    5
    6
    @SpringBootApplication
    public class StartupApplication {
    public static void main(String[] args) {
    SpringApplication.run(StartupApplication.class, args);
    }
    }
  2. 新增RouteController.java用于首页跳转
    1
    2
    3
    4
    5
    6
    7
    @Controller
    public class RouteController {
    @GetMapping("/")
    public String index(){
    return "index";
    }
    }
  3. webapp/staticpages目录移动到WEB-INF下并重命名为views
  4. index.html改成index.jsp并移动到views目录
  5. webapp/static目录移动到src/main/resources目录下
  6. novel.properties文件重命名为application.properties并新增如下内容
    1
    2
    3
    4
    5
    6
    7
    8
    server.port=18888
    # log配置文件
    logging.config=classpath:logback.xml
    # jsp配置
    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    # 静态文件配置
    spring.mvc.static-path-pattern=/static/**
  7. 删除pom.xml中spring-web-mvc相关依赖,并加如下内容
    1
    2
    3
    4
    5
    6
    7
    8
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
  8. 删除applicationContext.xmlservlet-context.xmlweb.xml三个文件
  9. 至此,项目结构上基本改造完成

遇到的问题

  1. Springboot项目编译正常启动Unable to start embedded Tomcat报错
    1
    2
    3
    4
    5
    6
    7
    仔细查看错误日志:
    Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

    通过依赖关系,你可以查到servlet-api.jar包,并发现低版本2.4的servlet-api.jar中没有getVirtualServerName()方法
    可尝试通过引入高版本(javax.servlet-api-3.1.0.jar)解决

    ps >> 本项目的解决方法是删除了该依赖jar包
  2. idea下启动正常访问,使用java -jar方式启动时,jsp页面报404
    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
    需要在pom.xml中加入如下内容:
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.4.0.RELEASE</version>
    </plugin>
    </plugins>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <includes>
    <include>**/**</include>
    </includes>
    <filtering>false</filtering>
    </resource>
    <resource>
    <directory>src/main/webapp</directory>
    <!--这里必须是META-INF/resources-->
    <targetPath>META-INF/resources</targetPath>
    <includes>
    <include>**/**</include>
    </includes>
    <filtering>false</filtering>
    </resource>
    </resources>
    </build>

    a. 网上说spring-boot-maven-plugin插件的版本必须小于1.4.2,一开始使用的1.5.x版本会报404
    b. 使用resource把webapp目录下的文件打包到META-INF/resources下,使用jar方式启动必须要这么处理,war方式不需要
  3. 静态资源文件访问报404错误
    1
    2
    3
    a. 静态文件目录需要放在 src/main/resources 目录下,尝试过放在webapp跟webapp/WEB-INF下均无法正常访问
    b. application.properties文件中需要配置静态资源文件的路径格式,如下:
    spring.mvc.static-path-pattern=/static/**

SpringMvc项目改造成springboot+web项目踩坑指南

https://trainoo.gitee.io/2019/12/18/springmvc-to-springboot-web/

作者

Trainoo

发布于

2019-12-18

更新于

2020-06-02

许可协议