2017-09-26 236 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 –