Spring boot 获取全局的Request 和Response

Spring boot
获取全局的Request 和Response


ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
HttpServ
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过自定义全局的拦截器或者切面来实现对查询的LocalDateTime减一的操作。 一种常见的方式是使用拦截器,在拦截器中对查询参数进行处理。首先,创建一个实现HandlerInterceptor接口的拦截器类,然后在preHandle方法中对请求进行处理。在处理过程中,可以获取到请求的参数,并对其中的LocalDateTime类型的参数进行减一操作。最后,将处理后的参数重新设置到请求中,使其继续传递给下一个处理器。 以下是一个简单的示例代码: ```java public class LocalDateTimeInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Map<String, String[]> parameterMap = request.getParameterMap(); for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { String[] values = entry.getValue(); for (int i = 0; i < values.length; i++) { if (values[i] != null && values[i].matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}")) { LocalDateTime dateTime = LocalDateTime.parse(values[i]); LocalDateTime modifiedDateTime = dateTime.minusDays(1); values[i] = modifiedDateTime.toString(); } } } return true; } } ``` 然后,在Spring Boot的配置类中注册该拦截器: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private LocalDateTimeInterceptor localDateTimeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localDateTimeInterceptor); } } ``` 这样,当有请求到达时,拦截器会对其中的LocalDateTime类型的参数进行减一操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值