SpringBoot的@GetMapping路徑匹配規(guī)則、國際化詳細(xì)教程
1. @GetMapping路徑匹配規(guī)則
- 默認(rèn)使用新版PathPatternParser進(jìn)行路徑匹配。性能比AntPathMatcher有6到8倍吞吐量提升,且降低30%~40%空間分配率兼容性方面。和antPathMatcher語法
- 兼容(但不能匹配**在中間的情況,但能匹配**在末尾的情況)。可以使用
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
進(jìn)行切換。默認(rèn)是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風(fēng)格的路徑模式語法具有以下規(guī)則:
- *:表示任意數(shù)量的字符
- ?:表示任意一個字符
- +: 表示1個或多個字符
- **:表示任意數(shù)量的目錄
- {}:表示一個命名的模式占位符
- []:表示字符集合,例如[a-z]表示小寫字母
例如:/{type}/{id}.html匹配任意文件名為{id}.html,在任意命名的{type}目錄下的文件
注意:Ant 風(fēng)格的路徑模式語法中的特殊字符需要轉(zhuǎn)義,如:
- 要匹配文件路徑中的星號,則需要轉(zhuǎn)義為\\*
- 要匹配文件路徑中的問號,則需要轉(zhuǎn)義為\\?
使用示例如下所示。訪問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類
實現(xiàn)步驟:
- 1.Spring Boot在資源路徑根目錄下查找messages資源綁定文件。文件名為:messages.properties
- 2.多語言可以定義多個消息文件,命名為messages_區(qū)域代碼.properties。如:
messages.properties:默認(rèn)。文件內(nèi)容如下:
login=Login sign=Sign-Up
messages_zh_CN.properties:中文環(huán)境。文件內(nèi)容如下:
login=登錄 sign=注冊
messages_en_US.properties:英語環(huán)境。文件內(nèi)容如下:
login=Login sign=Sign-Up
3.在頁面中可以使用表達(dá)式#{}
獲取國際化的配置項值。訪問頁面會自動根據(jù)瀏覽器的語言設(shè)置加載不同的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方式返回數(shù)據(jù)到頁面 return login; } }
5.application.properties國際化參數(shù)配置
spring.messages.basename=messages spring.messages.encoding=UTF-8
到此這篇關(guān)于SpringBoot的@GetMapping路徑匹配規(guī)則、國際化的文章就介紹到這了,更多相關(guān)SpringBoot @GetMapping路徑匹配規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot基于Sentinel在服務(wù)上實現(xiàn)接口限流
這篇文章主要介紹了SpringBoot基于Sentinel在服務(wù)上實現(xiàn)接口限流,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10idea創(chuàng)建包含多個springboot module的maven project的方法
這篇文章主要介紹了idea創(chuàng)建包含多個springboot module的maven project的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Quarkus中RESTEasy?Reactive集成合并master分支
這篇文章主要為大家介紹了Quarkus中RESTEasy?Reactive集成合并master分支的詳細(xì)作用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02使用spring+maven不同環(huán)境讀取配置方式
這篇文章主要介紹了使用spring+maven不同環(huán)境讀取配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系
這篇文章主要為大家詳細(xì)介紹了Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10MyBatis-Plus 批量插入數(shù)據(jù)的操作方法
spring boot+mybatis plus環(huán)境,單條插入用的是BaseMapper自帶的insert方法,本文重點給大家介紹MyBatis-Plus 批量插入數(shù)據(jù)的操作方法,感興趣的朋友一起看看吧2021-09-09Jpa中Specification的求和sum不生效原理分析
這篇文章主要為大家介紹了Jpa中Specification的求和sum不生效原理示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08