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);
                    }

                });
            }

        }
        ......省略部分......
    }
    ......省略部分......
}

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
    }

2. 国际化

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

实现步骤:

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

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

login=Login
sign=Sign-Up

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

login=登录
sign=注册

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

login=Login
sign=Sign-Up
  1. 在页面中可以使用表达式#{}获取国际化的配置项值。访问页面会自动根据浏览器的语言设置加载不同的messages消息配置文件
                <button type="button" class="btn btn-outline-light me-2" th:text="#{login}">Login</button>
                <button type="button" class="btn btn-warning" th:text="#{sign}">Sign-up</button>
  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. application.properties国际化参数配置
spring.messages.basename=messages
spring.messages.encoding=UTF-8
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值