gpt4 book ai didi

java - StringUtils 类型的方法 isNullOrEmpty(String) 未定义

转载 作者:行者123 更新时间:2023-12-02 01:21:33 31 4
gpt4 key购买 nike

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,但无法找到正确且良好的来源,然后我找到了显示此代码的网站,有人可以提供帮助:)

P.S 错误是 The method isNullOrEmpty(String) is undefined for the type StringUtils.. 在我在上面的代码中给出 *** 的行上

最佳答案

您正在使用org.springframework.util.StringUtils,它有一个方法isEmpty(value)。但是,您可能想要 Apache's Commons Lang它有一个更完整的 StringUtils。

关于java - StringUtils 类型的方法 isNullOrEmpty(String) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57620093/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com