SpringBoot的@GetMapping路径匹配规则、国际化详细教程
作者:Bulut0907
这篇文章主要介绍了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
3.在页面中可以使用表达式#{}获取国际化的配置项值。访问页面会自动根据浏览器的语言设置加载不同的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>4.在程序中可以自动注入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;
}
}5.application.properties国际化参数配置
spring.messages.basename=messages spring.messages.encoding=UTF-8
到此这篇关于SpringBoot的@GetMapping路径匹配规则、国际化的文章就介绍到这了,更多相关SpringBoot @GetMapping路径匹配规则内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
