# 路径工具
import org.springframework.util.AntPathMatcher;
AntPathMatcher matcher = new AntPathMatcher();
boolean match1 = matcher.match("/users/*", "/users/123"); // true
boolean match2 = matcher.match("/users/**", "/users/123/orders"); // true
boolean match3 = matcher.match("/user?", "/user1"); // true
// 提取路径变量
Map<String, String> vars = matcher.extractUriTemplateVariables("/users/{id}", "/users/42"); // {id=42}
# 字符匹配
import org.springframework.util.PatternMatchUtils;
boolean matches1 = PatternMat`chUtils.simpleMatch("user*", "username"); // true
boolean matches2 = PatternMatchUtils.simpleMatch("user?", "user1"); // true
boolean matches3 = PatternMatchUtils.simpleMatch(new String[]{"user*", "admin*"}, "username"); // true
System.out.println(matches1);
System.out.println(matches2);
System.out.println(matches3);`
# 集合
import org.springframework.util.CollectionUtils;
// 检查集合是否为空
boolean isEmpty = CollectionUtils.isEmpty(null); // true
boolean isEmpty2 = CollectionUtils.isEmpty(Collections.emptyList()); // true
// 集合操作
List<String> list1 = Arrays.asList("a", "b", "c");
List<String> list2 = Arrays.asList("b", "c", "d");
Collection<String> intersection = CollectionUtils.intersection(list1, list2); // [b, c]
// 合并集合
List<String> target = new ArrayList<>();
CollectionUtils.mergeArrayIntoCollection(new String[]{"a", "b"}, target);
# yaml工具
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
// 解析YAML文件
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml"));
Properties properties = yaml.getObject();
# 请求/响应包装器
- 请求包装器
- ContentCachingRequestWrapper:这是 Spring 提供的一个请求包装器,用于缓存请求的输入流。它允许多次读取请求体,这在需要多次处理请求数据(如日志记录和业务处理)时非常有用。
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class RequestWrapperFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
// 可以在这里处理请求数据
byte[] body = requestWrapper.getContentAsByteArray();
filterChain.doFilter(requestWrapper, response);
}
}
- 响应包装器
- ContentCachingResponseWrapper:这是 Spring 提供的一个响应包装器,用于缓存响应的输出流。它允许开发者在响应提交给客户端之前修改响应体,这在需要对响应内容进行后处理(如添加额外的头部信息、修改响应体)时非常有用。
@Component
public class ResponseWrapperFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
filterChain.doFilter(request, responseWrapper);
// 可以在这里处理响应数据
byte[] body = responseWrapper.getContentAsByteArray();
// 处理body,例如添加签名
responseWrapper.setHeader("X-Signature", "some-signature");
// 必须调用此方法以将响应数据发送到客户端
responseWrapper.copyBodyToResponse();
}
}