欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring mvc项目改造为Spring boot 问题

程序员文章站 2022-12-20 10:55:16
1. 打包方式jarwarjar的方式比较简单,是springboot 默认的。如果打包war, 部署到外部的tomcat, 需要做以下调整:启动类@SpringBootApplicationpublic class WebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplication...

1. 打包方式

  • jar
  • war

jar的方式比较简单,是springboot 默认的。

如果打包war, 部署到外部的tomcat, 需要做以下调整:

  • 启动类
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

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

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

    }

}
  • pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>               
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
</dependency>

<build>
    <finalName>appName</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.***.WebApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
   </plugins>
       
</build>
  • 去掉原web.xml

如果原来mvc项目包含web.xml文件,需要去掉,否则tomcat启动时日志会打印:启动一个或多个listener异常,导致tomcat启动失败,访问会报404。

因springboot 已经对web.xml 功能做了启动封装,无需web.xml文件,否则会有冲突。

参考:https://uhfun.cn/tech/2020/05/22/%E4%B8%BA%E4%BB%80%E4%B9%88SpringBoot%E9%A1%B9%E7%9B%AE%E4%B8%8D%E9%9C%80%E8%A6%81web.xml.html

2. Spring Boot设置访问url接口后缀

传统的xml配置方式

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Spring Boot配置方式

application.yml

spring:
  mvc:
    contentnegotiation:
      favor-path-extension: true
    pathmatch:
      use-suffix-pattern: true
      use-registered-suffix-pattern: true

启动加载配置类

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * -设置url后缀模式匹配规则
     * -该设置匹配所有的后缀,使用.do或.action都可以
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(true) //设置是否是后缀模式匹配,即:/test.*
                .setUseTrailingSlashMatch(true) //设置是否自动后缀路径模式匹配,即:/test/
                .setUseRegisteredSuffixPatternMatch(false); //开启路径后缀匹配
    }

    /**
     * -该设置严格指定匹配后缀*.do或.action,但有风险
     * @param dispatcherServlet servlet调度器
     * @return ServletRegistrationBean
     */
//    @Bean
    public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
    	ServletRegistrationBean<DispatcherServlet> servletServletRegistrationBean = new ServletRegistrationBean<>(dispatcherServlet);
        servletServletRegistrationBean.addUrlMappings("*.do");//指定.do后缀,可替换其他后缀
        return servletServletRegistrationBean;
    }

配置类使用上面第一种方式即可,第二种本人试了有问题,无法正常访问。

 

本文地址:https://blog.csdn.net/qq_27641935/article/details/109578207