@Component
public class FilterDemo extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (StringUtils.equals("/findAll",request.getRequestURI())) {
String name = request.getParameter("name");//ajax请求穿值
if (!StringUtils.isEmpty(name)) {
if (name.equals("1234")) {
filterChain.doFilter(request,response);
}else{
/** 设置contenttype为json ***/
response.setContentType("application/json");
/** 设置编码为utf-8 **/
response.setCharacterEncoding("UTF-8");
/** 将需要返回的对象转换为json ***/
String str = "gson.toJson(jsonObject)";
Map map = new HashMap();
map.put("name","data");
/** 写入json **/
response.getWriter().write(JSON.toJSONString(map));
return;
}
}
}
filterChain.doFilter(request,response);
}
ajax请求 ,过滤器拦截后返回给ajax请求
最新推荐文章于 2021-08-06 21:07:02 发布
本文介绍了一个使用Spring框架的Filter实现,特别关注了如何通过OncePerRequestFilter处理特定URL请求,如/findAll,并根据参数name的值进行过滤逻辑。当name等于1234时,请求将直接传递;否则,将返回一个包含name:data的JSON响应。
942

被折叠的 条评论
为什么被折叠?



