实现客户端以调用Rest Web服务

时间:2015-07-01 19:30:06

标签: java web-services rest

我已经实现了http客户端与我使用Apache CXF实现的其他服务器进行通信。当我在浏览器中使用url时,evrything很好,但是当我使用client时,我得到了一个例外。

是个例外
  

线程“main”中的异常java.lang.NoSuchMethodError:org.slf4j.spi.LocationAwareLogger.log(Lorg / slf4j / Marker; Ljava / lang / String; ILjava / lang / String; Ljava / lang / Throwable;) V       在org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:99)       在org.apache.http.client.protocol.RequestAuthCache.process(RequestAuthCache.java:75)       at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:131)       在org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:193)       在org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)       在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)       在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)       在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)       在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)       在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)       在ABC.XYZ(ABC.java:45)       在ABC.XYZ(ABC.java:93)

我在客户端编写了以下代码

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet getRequest = new HttpGet(path);
    getRequest.addHeader("accept", "application/xml");
    HttpResponse response = client.execute(getRequest);//here I have an exception

2 个答案:

答案 0 :(得分:0)

我建议你使用maven并添加apache http客户端依赖项。可能是slf4j.jar的不兼容版本存在导致问题。 maven将能够处理兼容版本。

答案 1 :(得分:0)

我认为请求有些不同,而第二个请求需要记录器。您应该正确地向slf4j添加依赖项并查看错误