2017-09-26 171 views
1

我正在尝试为某些Rest应用程序创建客户端。我在Chrome中使用高级REST客户端测试了其他应用程序。客户端响应应用程序.Json

我收到:

{ 
    "authorization": false, 
    "provider": "Provider" 
} 

即好。 但我想在我的客户获得此:

λ java -jar Client_consumer-1.0-SNAPSHOT.jar 
or[email protected]6591f517 

类是:

@XmlRootElement 
public class Auth_response implements Serializable { 
private String Provider; 
private boolean authorization; 

public Auth_response() { 
    super(); 
} 

public Auth_response(String Provider, boolean authorization) { 
    super(); 
    this.Provider = Provider; 
    this.authorization = authorization; 
} 

public String getProvider() { 
    return Provider; 
} 

public boolean isAuthorization() { 
    return authorization; 
} 

public void setProvider(String Provider) { 
    this.Provider = Provider; 
} 

public void setAuthorization(boolean authorization) { 
    this.authorization = authorization; 
} 

@Override 
public String toString() { 
    return "Auth_response{" + "Provider=" + Provider + ", authorization=" + authorization + '}'; 
} 

}

public class MainApp extends Application { 
    public static final String BASE_URI = "http://localhost:8000/test"; 
    public static final String PATH_NAME= "/sytem/Consumer/r/Provider"; 

@Override 
public void start(Stage stage) throws Exception { 
} 

public static void main(String[] args) { 

    Client client = ClientBuilder.newClient(); 
    WebTarget target = client.target(BASE_URI).path(PATH_NAME); 
    Response response = target.request(MediaType.APPLICATION_JSON).get(); 
    System.out.println(response); 
    client.close(); 

} 

} 

我只在终端接收此想知道如何正确打印响应,以及如何再次转换为java对象。我尝试了一些例子,但没有任何作品,我认为我需要一些指导。

编辑: 我想这样来的对象:

Auth_response r= 
client.target("http://localhost:8000/test/system/Consumer/r/Provider") 
.request(MediaType.APPLICATION_JSON_TYPE). 
get(Auth_response.class); 
    System.out.println("funciona:"); 
    System.out.println(r.getProvider()); 
    System.out.println(r.isAuthorization()); 

但我得到一个错误:

Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class ah.client_consumer.Auth_response 
    at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42) 
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:75) 
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:52) 
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59) 
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181) 
    at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:213) 
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:105) 
    ... 14 more 

异常运行的应用程序ah.client_consumer.MainApp

+0

误差'致HTTP 404未找到'表示您尝试访问的Web服务无法访问。你可以请张贴那部分吗? –

+0

当我使用高级客户端休息时,Web服务工作。 –

+0

我在路径中发生了错误,我编辑了当前错误的错误。 –

回答

0

尝试改变下线:

Response response = target.request(MediaType.APPLICATION_JSON).get(); 
System.out.println(response); 

Response response = target.request(MediaType.APPLICATION_JSON).get(String.class); 
System.out.println(response.toString()); 

因为你还没有指定您接受什么类型的反应,你得到对象作为响应。

+0

我试过,我获得:or[email protected]6591f517 –