V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  iiicarus  ›  全部回复第 7 页 / 共 8 页
回复总数  143
1  2  3  4  5  6  7  8  
2019-09-09 14:27:51 +08:00
回复了 jcloud 创建的主题 推广 回帖送京东 JOY 玩偶,最高还能领 100000 京豆!
想要中奖,就是这么直接!
2019-09-09 12:02:50 +08:00
回复了 iiicarus 创建的主题 Java 求教,如何从 HttpServletRequest 中获取类名和方法名
代码如上
2019-09-09 12:02:31 +08:00
回复了 iiicarus 创建的主题 Java 求教,如何从 HttpServletRequest 中获取类名和方法名
@Slf4j
public class HttpTraceLogFilter extends OncePerRequestFilter implements Ordered {

private static final String NEED_TRACE_PATH_PREFIX_BUSINESS = "/business";
private static final String NEED_TRACE_PATH_PREFIX_NPI = "/npi";
private static final String NEED_TRACE_PATH_PREFIX_SYSTEM = "/system";
private static final String IGNORE_CONTENT_TYPE = "multipart/form-data";

private final MeterRegistry registry;

public HttpTraceLogFilter(MeterRegistry registry) {
this.registry = registry;
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 10;
}

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (!isRequestValid(request)) {
filterChain.doFilter(request, response);
return;
}
if (!(request instanceof ContentCachingRequestWrapper)) {
request = new ContentCachingRequestWrapper(request);
}
if (!(response instanceof ContentCachingResponseWrapper)) {
response = new ContentCachingResponseWrapper(response);
}

int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
long startTime = System.currentTimeMillis();
try {
filterChain.doFilter(request, response);
status = response.getStatus();
} finally {
String path = request.getRequestURI();
boolean pathFlag = path.startsWith(NEED_TRACE_PATH_PREFIX_BUSINESS) || path.startsWith(NEED_TRACE_PATH_PREFIX_NPI) || path.startsWith(NEED_TRACE_PATH_PREFIX_SYSTEM);
if (pathFlag && !Objects.equals(IGNORE_CONTENT_TYPE, request.getContentType())) {

String requestBody = IOUtils.toString(request.getInputStream(), Charsets.UTF_8);
log.info(requestBody);
//1. 记录日志
HttpTraceLog traceLog = new HttpTraceLog();
traceLog.setPath(path);
log.info("path ===================>: {}", path);
traceLog.setMethod(request.getMethod());
log.info("method ===================>: {}", request.getMethod());
long timeConsuming = System.currentTimeMillis() - startTime;
traceLog.setTimeTaken(timeConsuming);
log.info("timeConsuming ===================>: {} ms", timeConsuming);
//traceLog.setTime(LocalDateTime.now().toString());
//traceLog.setParameterMap(JsonMapper.INSTANCE.toJson(request.getParameterMap()));
traceLog.setStatus(status);
log.info("status ===================>: {}", status);
//traceLog.setRequestBody(getRequestBody(request));
//traceLog.setResponseBody(getResponseBody(response));
//log.info("Http trace log: {}", JsonMapper.INSTANCE.toJson(traceLog));
//log.info("Http trace log: {}===============================================>", traceLog);
}
updateResponse(response);
}
}

private boolean isRequestValid(HttpServletRequest request) {
try {
new URI(request.getRequestURL().toString());
return true;
} catch (URISyntaxException ex) {
return false;
}
}

private String getRequestBody(HttpServletRequest request) {
String requestBody = "";
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
try {
requestBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return requestBody;
}

private String getResponseBody(HttpServletResponse response) {
String responseBody = "";
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
if (wrapper != null) {
try {
responseBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
} catch (IOException e) {
// NOOP
}
}
return responseBody;
}

private void updateResponse(HttpServletResponse response) throws IOException {
ContentCachingResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
Objects.requireNonNull(responseWrapper).copyBodyToResponse();
}


@Data
private static class HttpTraceLog {
private String path;
private String parameterMap;
private String method;
private Long timeTaken;
private String time;
private Integer status;
private String requestBody;
private String responseBody;
}
}
2019-09-09 12:02:14 +08:00
回复了 iiicarus 创建的主题 Java 求教,如何从 HttpServletRequest 中获取类名和方法名
@Configuration
@ConditionalOnWebApplication
public class HttpTraceConfiguration {

@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class ServletTraceFilterConfiguration {

@Bean
public HttpTraceLogFilter httpTraceLogFilter(MeterRegistry registry) {
return new HttpTraceLogFilter(registry);
}
}
}
2019-08-31 16:46:59 +08:00
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@7654 get! 谢谢
2019-08-31 16:46:43 +08:00
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@lhx2008 谢谢。以后就这样读。读英文可能有的人不喜欢
2019-08-31 16:46:10 +08:00
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
@yidinghe 好的 谢谢
2019-08-31 16:34:49 +08:00
回复了 iiicarus 创建的主题 问与答 B-Tree 和 B+Tree 怎么读
希望大家不要笑我
2019-08-30 11:22:19 +08:00
回复了 xduanx 创建的主题 问与答 为了健身,为了老婆,为了孩子,干了一个月 996,辞职了
@xduanx 我的 40 米长大刀呢?
2019-08-30 11:19:18 +08:00
回复了 5200 创建的主题 问与答 最近想学双拼了, v 友们推荐哪家?
在用小鹤,搜狗输入法就有。一个星期了,就是自己不太熟练,打字有点慢,熟练了双拼很快
2019-08-30 10:04:04 +08:00
回复了 wsy190 创建的主题 程序员 写程序这么精简真的好吗?
这样挺好的
@gy911201 肯定是合法的 不然不会这么做 所以无奈卸载 qq
@codermagefox 有的 可以看到
@SeanCheng qq 会被监控到
@LxExExl 武汉的
1  2  3  4  5  6  7  8  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5686 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 23ms · UTC 06:29 · PVG 14:29 · LAX 22:29 · JFK 01:29
Developed with CodeLauncher
♥ Do have faith in what you're doing.