扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
小编给大家分享一下AOP与Filter拦截请求打印日志的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
公司主营业务:成都网站建设、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出合山免费做网站回馈大家。
1 . Http请求拦截器(Filter) : 从HttpServletRequest获取基本的请求信息
import name.ealen.util.HttpUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by EalenXie on 2018/9/7 15:56. * Http请求拦截器,日志打印请求基本相关信息 */ @Configuration public class FilterConfiguration { private static final Logger log = LoggerFactory.getLogger(FilterConfig.class); @Bean @Order(Integer.MIN_VALUE) @Qualifier("filterRegistration") public FilterRegistrationBean filterRegistration() { FilterRegistrationBeanregistration = new FilterRegistrationBean<>(); registration.setFilter(controllerFilter()); registration.addUrlPatterns("/*"); return registration; } private Filter controllerFilter() { return new Filter() { @Override public void init(FilterConfig filterConfig) { log.info("ControllerFilter init Success"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String requestId = request.getHeader("Request-Id"); if (requestId == null) requestId = request.getRequestedSessionId(); System.out.println(); log.info("Http Request Request-Id : " + requestId); log.info("Http Request Information : {\"URI\":\"" + request.getRequestURL() + "\",\"RequestMethod\":\"" + request.getMethod() + "\",\"ClientIp\":\"" + HttpUtil.getIpAddress(request) + "\",\"Content-Type\":\"" + request.getContentType() + "\"}"); chain.doFilter(request, response); } @Override public void destroy() { log.info("ControllerFilter destroy"); } }; } }
2 . Controller的拦截AOP : 获取 请求的对象,请求参数,返回数据,请求返回状态,内部方法耗时。
import com.alibaba.fastjson.JSON; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * Created by EalenXie on 2018/9/7 14:19. * AOP打印日志 : 请求的对象,请求参数,返回数据,请求状态,内部方法耗时 */ @Aspect @Component public class ControllerInterceptor { private static final Logger log = LoggerFactory.getLogger(ControllerInterceptor.class); @Resource private Environment environment; @Around(value = "execution (* name.ealen.web.*.*(..))") public Object processApiFacade(ProceedingJoinPoint pjp) { String appName; try { appName = environment.getProperty("spring.application.name").toUpperCase(); } catch (Exception e) { appName = "UNNAMED"; } long startTime = System.currentTimeMillis(); String name = pjp.getTarget().getClass().getSimpleName(); String method = pjp.getSignature().getName(); Object result = null; HttpStatus status = null; try { result = pjp.proceed(); log.info("RequestTarget : " + appName + "." + name + "." + method); log.info("RequestParam : " + JSON.toJSON(pjp.getArgs())); if (result instanceof ResponseEntity) { status = ((ResponseEntity) result).getStatusCode(); } else { status = HttpStatus.OK; } } catch (Throwable throwable) { status = HttpStatus.INTERNAL_SERVER_ERROR; result = new ResponseEntity<>("{\"Internal Server Error\" : \"" + throwable.getMessage() + "\"}", status); throwable.printStackTrace(); } finally { log.info("ResponseEntity : {" + "\"HttpStatus\":\"" + status.toString() + "\"" + ",\"ResponseBody\": " + JSON.toJSON(result) + "}"); log.info("Internal Method Cost Time: {}ms", System.currentTimeMillis() - startTime); } return result; } }
3 . 提供一个简单的restfull接口 :
package name.ealen.web; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by EalenXie on 2018/9/7 14:24. */ @RestController public class SayHelloController { @RequestMapping("/sayHello") public String sayHello() { return "hello world"; } @RequestMapping("/say") public ResponseEntity> say(@RequestBody Object o) { return new ResponseEntity<>(o, HttpStatus.OK); } }
4 . 使用Postman进行基本测试 :
5 . 控制台可以看到基本效果 :
以上是“AOP与Filter拦截请求打印日志的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流