> -----Original Message-----
> From: Vitaly Peressada [mailto:[email protected]]
> Sent: Tuesday, February 02, 2010 11:29 AM
> To: [email protected]
> Subject: Capture Serialized Response
>
> I am using JAX-RS and want to capture serialized HTTP output after CXF
> serializes response as per configured content-type. I tried my own
> ResponseHandler but could not get the hold on output stream. I think
> serialization happens later in CXF chain.
Are you doing this for testing sake, or do you really have an
application need for this?
If the latter, I suppose you could implement a servlet filter, although
Sergey or others may have better ideas.
If you're doing this for testing, this is something I had to figure out
a while ago (and you can find some more details by searching this list
for my question about this ("How can I make WebClient in test return raw
XML or JSON?"). This uses an embedded Jetty instance.
I use something like this in the test class:
---------------------
@BeforeClass
public static void startServer() {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setServiceBeans(catalogContentController);
sf.getInInterceptors().add(new LoggingInInterceptor());
sf.getOutInterceptors().add(new LoggingOutInterceptor());
sf.setProvider(new JacksonJsonProvider());
sf.setAddress(BASE_SERVICE_URI);
Map<Object, Object> mappings = new HashMap<Object, Object>();
mappings.put("xml", "application/xml");
mappings.put("json", "application/json");
sf.setExtensionMappings(mappings);
JacksonInit jacksonInit = new JacksonInit();
ObjectMapper objectMapper = new ObjectMapper();
jacksonInit.setObjectMapper(objectMapper);
jacksonInit.setAnnotationIntrospector(new
AnnotationIntrospector.Pair(new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector()));
jacksonInit.init();
sf.create();
}
-----------------
Then my individual tests can do something like this:
------------------------
WebClient client = WebClient.create(BASE_SERVICE_URI);
client.path("/catalog/catalog/" + id);
catalog = client.get(Catalog.class);
assertEquals(id, catalog.getId());
Response response = client.get();
System.out.println("response[" + response + "]");
-----------------
(Sergey's response to my question about this suggested doing
"client.get(Response.class)", but this appears to do the same thing.)
The Response object gives you a stream to read from and HTTP headers.