与Apache HTTP客户端和日志记录问题(Issues with Apache HTTP Cli

2019-10-19 03:08发布

在我的Tapestry应用,我需要做一些点的GET请求到一定的URL。 我不喜欢这样(使用Apache的HttpClient的4.2.1)

String scheme = getScheme();
String host = getHost();
int port = getPort();
String path = getPath();
String paramKey = getParamKey();
String paramValue = getParamValue();

URIBuilder builder = new URIBuilder();
builder
    .setScheme(scheme)
    .setHost(host)
    .setPort(port)
    .setPath(path)
    .setParameter(paramKey, paramValue);

URI uri = builder.build();

HttpGet getRequest = new HttpGet(uri);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(getRequest);

当我部署我在Glassfish(3.1.2.2)WAR和执行有问题的代码,以下异常被抛出:

Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category) (Caused by org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category))
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)

我在pom.xml中指定该

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.1</version>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

我检查,确信是“不被人发现的”类实际上是在WEB-INF / lib目录/ log4j的-1.2.14。

有什么办法来解决这个问题?

Answer 1:

你有一个类加载器的问题。 也许其他网络应用,使用不同的log4j的版本,第一次加载。 类加载器使用它找到的类的第一个版本。 尝试这个:

  • 卸下应用服务器所有其他应用程序
  • 检查你没有在服务器的类路径中另一个log4j的。

尝试安装和启动应用程序。



文章来源: Issues with Apache HTTP Client and logging