• SpringBoot的@GetMapping路径匹配规则、国际化


    1. @GetMapping路径匹配规则

    • 默认使用新版PathPatternParser进行路径匹配。性能比AntPathMatcher有6到8倍吞吐量提升,且降低30%~40%空间分配率
    • 兼容性方面。和antPathMatcher语法兼容(但不能匹配**在中间的情况,但能匹配**在末尾的情况)。可以使用spring.mvc.pathmatch.matching-strategy=ant_path_matcher进行切换。默认是path_pattern_parser。查看源码如下:
    package org.springframework.boot.autoconfigure.web.servlet;
    ......省略部分......
    public class WebMvcAutoConfiguration {
        ......省略部分......
        public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
            ......省略部分......
            public void configurePathMatch(PathMatchConfigurer configurer) {
                if (this.mvcProperties.getPathmatch().getMatchingStrategy() == MatchingStrategy.ANT_PATH_MATCHER) {
                    configurer.setPathMatcher(new AntPathMatcher());
                    this.dispatcherServletPath.ifAvailable((dispatcherPath) -> {
                        String servletUrlMapping = dispatcherPath.getServletUrlMapping();
                        if (servletUrlMapping.equals("/") && this.singleDispatcherServlet()) {
                            UrlPathHelper urlPathHelper = new UrlPathHelper();
                            urlPathHelper.setAlwaysUseFullPath(true);
                            configurer.setUrlPathHelper(urlPathHelper);
                        }
    
                    });
                }
    
            }
            ......省略部分......
        }
        ......省略部分......
    }
    
    • 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

    Ant风格的路径模式语法具有以下规则:

    • *:表示任意数量的字符
    • ?:表示任意一个字符
    • +: 表示1个或多个字符
    • **:表示任意数量的目录
    • {}:表示一个命名的模式占位符
    • []:表示字符集合,例如[a-z]表示小写字母

    例如:/{type}/{id}.html匹配任意文件名为{id}.html,在任意命名的{type}目录下的文件

    注意:Ant 风格的路径模式语法中的特殊字符需要转义,如:

    • 要匹配文件路径中的星号,则需要转义为\\*
    • 要匹配文件路径中的问号,则需要转义为\\?

    使用示例如下所示。访问http://localhost:8080/abc/bd/abcdef/1/2

        @GetMapping("/a*/b?/{p1:[a-f]+}/**")
        public String hello2(HttpServletRequest request, @PathVariable("p1") String path) {
    
            log.info("路径变量p1: {}", path);    // 路径变量p1: abcdef
            String uri = request.getRequestURI();
            return uri;    // /abc/bd/abcdef/1/2
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. 国际化

    国际化的自动配置参照MessageSourceAutoConfiguration类

    实现步骤:

    1. Spring Boot在资源路径根目录下查找messages资源绑定文件。文件名为:messages.properties
    2. 多语言可以定义多个消息文件,命名为messages_区域代码.properties。如:

    messages.properties:默认。文件内容如下:

    login=Login
    sign=Sign-Up
    
    • 1
    • 2

    messages_zh_CN.properties:中文环境。文件内容如下:

    login=登录
    sign=注册
    
    • 1
    • 2

    messages_en_US.properties:英语环境。文件内容如下:

    login=Login
    sign=Sign-Up
    
    • 1
    • 2
    1. 在页面中可以使用表达式#{}获取国际化的配置项值。访问页面会自动根据浏览器的语言设置加载不同的messages消息配置文件
                    
                    
    
    • 1
    • 2
    1. 在程序中可以自动注入MessageSource组件,获取国际化的配置项值
    package com.hh.springboot3test.controller;
    
    import jakarta.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.MessageSource;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Locale;
    
    
    
    @RestController
    public class ControllerTest {
    
        @Autowired  // 国际化取消息用的组件
        MessageSource messageSource;
    
        @GetMapping("/global")
        public String global(HttpServletRequest request) {
    
            Locale locale = request.getLocale();
            // 利用代码的方式获取国际化配置文件中指定的配置项的值
            String login = messageSource.getMessage("login", null, locale);
    
            // 以rest方式返回数据到页面
            return login;
        }
    
    }
    
    
    • 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
    1. application.properties国际化参数配置
    spring.messages.basename=messages
    spring.messages.encoding=UTF-8
    
    • 1
    • 2
  • 相关阅读:
    【工作技术栈】【源码阅读】解决restTemplate提取LocalDateTime等特殊类时序列化报错问题
    2.8 Go语言中的for循环, break和continue
    spring.profiles.active和spring.profiles.include的使用及区别说明
    用于人类复杂疾病成药性评估的R包:DREAM包
    Vite HMR API
    linux笔记(5):按照东山派的官方教程编译buildroot(东山哪吒,D1-H)踩坑记录
    Git详解及 github使用
    【力扣】两数相加
    基于springboot实现房源出租信息系统演示【附项目源码+论文说明】
    基于Java的酒店管理系统设计与实现
  • 原文地址:https://blog.csdn.net/yy8623977/article/details/131778528