SpringBoot的@GetMapping路徑匹配規(guī)則、國際化詳細教程
1. @GetMapping路徑匹配規(guī)則
- 默認使用新版PathPatternParser進行路徑匹配。性能比AntPathMatcher有6到8倍吞吐量提升,且降低30%~40%空間分配率兼容性方面。和antPathMatcher語法
- 兼容(但不能匹配**在中間的情況,但能匹配**在末尾的情況)??梢允褂?code>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風格的路徑模式語法具有以下規(guī)則:
- *:表示任意數(shù)量的字符
- ?:表示任意一個字符
- +: 表示1個或多個字符
- **:表示任意數(shù)量的目錄
- {}:表示一個命名的模式占位符
- []:表示字符集合,例如[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類
實現(xiàn)步驟:
- 1.Spring Boot在資源路徑根目錄下查找messages資源綁定文件。文件名為:messages.properties
- 2.多語言可以定義多個消息文件,命名為messages_區(qū)域代碼.properties。如:
messages.properties:默認。文件內(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.在頁面中可以使用表達式#{}獲取國際化的配置項值。訪問頁面會自動根據(jù)瀏覽器的語言設置加載不同的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
到此這篇關于SpringBoot的@GetMapping路徑匹配規(guī)則、國際化的文章就介紹到這了,更多相關SpringBoot @GetMapping路徑匹配規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java class文件格式之數(shù)據(jù)類型(二)_動力節(jié)點Java學院整理
這篇文章主要介紹了Java class文件格式之數(shù)據(jù)類型(二)的相關資料,需要的朋友可以參考下2017-06-06

