import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.MDC;
import java.util.UUID;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
* A webflow request interceptor injecting correlation id to the request context.
*/
public class TestController extends HandlerInterceptorAdapter {
private static final String CORRELATION_ID_HEADER_NAME = "X-Correlation-Id";
private static final String CORRELATION_ID_LOG_VAR_NAME = "correlationId";
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,
final Object handler) throws Exception {
final String correlationId = getCorrelationIdFromHeader(request);
MDC.put(CORRELATION_ID_LOG_VAR_NAME, correlationId);
return true;
}
@Override
public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response,
final Object handler, final Exception ex) throws Exception {
MDC.remove(CORRELATION_ID_LOG_VAR_NAME);
}
private String getCorrelationIdFromHeader(final HttpServletRequest request) {
String correlationId = request.getHeader(CORRELATION_ID_HEADER_NAME);
***if (StringUtils.isNullOrEmpty(correlationId)) {***
correlationId = generateUniqueCorrelationId();
}
return correlationId;
}
private String generateUniqueCorrelationId() {
return UUID.randomUUID().toString();
}
}
我试图运行这个程序,但错误来了,你能帮我吗?我正在学习有关日志记录的相关 ID,但找不到正确且良好的来源,然后我找到了显示此代码的网站,有人可以帮忙:)
PS 错误是 The method isNullOrEmpty(String) is undefined for the type StringUtils.. 在我在上面的代码中给出 *** 的那一行