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();
}
}
i tried to run this program but the error comes can u help me? i am learning correlation ID on logging but cant find the right and good source then i found site which show this code can someone help:)
P.S the error is The method isNullOrEmpty(String) is undefined for the type StringUtils.. on the line where i give *** on the code above
You are using
org.springframework.util.StringUtilswhich has a methodisEmpty(value). However, you probably want Apache's Commons Lang which has a much more complete StringUtils.