Rest 如何使用org.apache.cxf.jaxrs.client.WebClient而不获取ServerWebApplicationException?

Rest 如何使用org.apache.cxf.jaxrs.client.WebClient而不获取ServerWebApplicationException?,rest,cxf,jax-rs,Rest,Cxf,Jax Rs,我使用的是来自tomEE plus 1.7.1的Apache CXF WebClient。 cxf-rt-frontend-jaxrs-2.6.14.jar 我实现了一个REST客户端,它需要JSON服务器响应: public RestClient(String aBaseUrl, String aPath) { this.client = WebClient.create(aBaseUrl); this.client.path(aPath); this.client.

我使用的是来自tomEE plus 1.7.1的Apache CXF WebClient。 cxf-rt-frontend-jaxrs-2.6.14.jar

我实现了一个REST客户端,它需要JSON服务器响应:

public RestClient(String aBaseUrl, String aPath) {

    this.client = WebClient.create(aBaseUrl);
    this.client.path(aPath);
    this.client.accept(MediaType.APPLICATION_JSON);
}

public JSONObject post(MultivaluedMap<String, String> params) {

    LOG.debug("sending POST request to: " + this.client.getCurrentURI());
    LOG.debug("POST parameters: " + PrivacyFilter.getLogLineFor(params));

    Form theForm = new Form(params);        
    JSONObject theJson;
    String resp = this.client.post(theForm, String.class);

    try {
        theJson = new JSONObject(resp);             
    } catch (JSONException e) {
        LOG.warn("got malformed answer from Server - cannot parse as JSON");
        LOG.trace(resp);
        throw new RuntimeException("got malformed answer from Server - cannot parse as JSON", e);
    }

    return theJson;
}
原因是什么?RESTful API也使用大于400的代码进行应答,这不是很正常吗?在我看来,在这种情况下,例外情况并不是一个好的解决办法

我没有弄清楚如何使用CXF实现我的REST客户机。是否有可能改变这种行为?仅当responseClass!=回答:课堂。我以这种方式修改了REST客户端:

public JSONObject post(MultivaluedMap<String, String> params) {

    LOG.debug("sending POST request to: " + this.client.getCurrentURI());
    LOG.debug("POST paramters: " + PrivacyFilter.getLogLineFor(params));

    Form theForm = new Form(params);        
    JSONObject theJson;

    Response respObject = this.client.post(theForm, Response.class);

    // HOW TO GET THE HTTP BODY OUT OF reps?!

    try {
        theJson = new JSONObject(resp);             
    } catch (JSONException e) {
        LOG.warn("got malformed answer from Server - cannot parse as JSON");
        LOG.trace(resp);
        throw new RuntimeException("got malformed answer from Server - cannot parse as JSON", e);
    }

    return theJson;
}
如果我调用它,我会得到一个sun.net.www.protocol.http.HttpURLConnection$HttpInputStream的实例,它并没有真正的帮助

您知道如何使用CXF实现REST客户端,在HTTP状态代码大于400的情况下不会崩溃吗?

我发现了这个问题。 问题出在方法中的org.apache.cxf.jaxrs.client.AbstractClient实现内部

protected <T> T readBody(Response r, Message outMessage, Class<T> cls, Type type, Annotation[] anns);
我在pom.xml中添加了以下内容:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>2.6.16</version>
    <scope>compile</scope>
</dependency>   
因此,我无法使用tomEE plus 1.7.1中的CXF WebClient。有什么建议吗?我将尝试改用apache HTTP客户端:

protected <T> T readBody(Response r, Message outMessage, Class<T> cls, Type type, Annotation[] anns);
/* CXF does it for HTTP codes >400 
         * 
         * the original CXF (cxf-rt-frontend-jaxrs-2.6.14.jar) from tomEE 1.7.1 
         * does not provide the HTTP body in >400 case.
         * 
         *  We assume the usage of cxf-rt-frontend-jaxrs-2.6.16.jar here
         */
        catch (ServerWebApplicationException e) {

            resp = e.getMessage(); // here is the HTTP body!
        }
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>2.6.16</version>
    <scope>compile</scope>
</dependency>   
java.lang.ExceptionInInitializerError
...
    Caused by: java.lang.IllegalArgumentException: interface org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy is not visible from class loader