2012-05-15 23 views
5

C'è un modo sempliceStampa Jersey JSON in Unit Testing

  • per stampare un JAXB annotato esempio
  • in formato JSON (come esattamente jersey stamperà in AS)
  • durante l'esecuzione di test di unità
  • senza alcun server incorporato avviato?

Posso eseguire e visualizzare in XML con JAXBContext, Marshaller e così via.

C'è qualche esempio per JSON?


L'ho trovato. È simile a JAXB.

Con Jersey API

final JSONJAXBContext context = new JSONJAXBContext(...); 

final JSONMarshaller marshaller = context.createJSONMarshaller(); 
marshaller.marshallToJSON(...); 

final JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller(); 
final T unmarshalled = unmarshaller.unmarshalJAXBElementFromJSON(
     ..., T.class).getValue(); 

per Maven

<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-json</artifactId> 
    <scope>test</scope> 
</dependency> 

risposta

2

E 'possibile:

... 
StringWriter writer = new StringWriter(); 
marshaller.marshallToJSON(objectToMarshall, writer) 
logger.debug(writer.toString()); 
... 

È possibile utilizzare un StringWriter per prendere il String rappresentazione dell'output JSON marshalled.