2015-11-17 6 views
1

Comment annuler une chaîne de réponse JSON dans l'objet correct en utilisant le client de repos Apache CXF?Réponse unmarsal du client Apache CXF

Ci-dessous est ma mise en œuvre qui appelle le point de fin de repos. J'utilise Apache CXF 2.6.14

S'il vous plaît ne pas que l'état de la réponse me dira quel objet se déchaîner dans.

public Object redeem(String client, String token) throws IOException { 
    WebClient webClient = getWebClient(); 
    webClient.path(redeemPath, client); 

    Response response = webClient.post(token); 
    InputStream stream = (InputStream) response.getEntity(); 

    //unmarshal the value 
    String value = IOUtils.toString(stream); 

    if (response.getStatus() != 200) { 
     //unmarshall into Error object and return 
    } else { 
     //unmarshall into Token object and return 
    } 
} 

Répondre

0

Ma solution. Je cours le projet dans un serveur de Tomee.

A l'intérieur du dossier Tomee lib, le projet est fourni avec une librairie jettison.

serveurs/apache-tomee-1.7.1-jaxrs/lib/jettison-1.3.4.jar

On peut utiliser le JSONObject qui est à l'intérieur du largage lib en combinaison avec JAXBContext pour analyser la chaîne JSON renvoyée.

public Object redeem(String client, String token) throws Exception { 
    WebClient webClient = getWebClient(); 
    webClient.path(redeemPath, client); 

    Response response = webClient.post(token); 
    InputStream stream = (InputStream) response.getEntity(); 

    //unmarshal the value 
    String value = IOUtils.toString(stream); 

    //use the json object from the jettison lib which is located in the Tomee lib folder. 
    JSONObject jsonObject = new JSONObject(value); 

    if (response.getStatus() != 200) { 

     JAXBContext jc = JAXBContext.newInstance(ResourceError.class); 
     XMLStreamReader reader = new MappedXMLStreamReader(jsonObject); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 

     ResourceError resourceError = (ResourceError) unmarshaller.unmarshal(reader); 
     return resourceError; 

    } else { 

     JAXBContext jc = JAXBContext.newInstance(Token.class); 
     XMLStreamReader reader = new MappedXMLStreamReader(jsonObject); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 

     Token token = (Token) unmarshaller.unmarshal(reader); 
     return token; 
    } 
}