rest - How to get plain XML from javax.ws.rs Entity on client side -
i have rest
client creates xml
entity via entity.entity(myobject, mediatype.application_xml_type)
. after that, call webtarget.request().buildpost(...
how can request body xml
client send server? (i need debugging reasons.) here entity
object.
of course serialize myself marshaller
same xml
client send?
you have clientrequestfilter
following, simplified version of jersey's loggingfilter
:
import java.io.bytearrayoutputstream; import java.io.filteroutputstream; import java.io.ioexception; import java.io.outputstream; import java.nio.charset.charset; import java.nio.charset.standardcharsets; import java.util.logging.logger; import javax.annotation.priority; import javax.ws.rs.constrainedto; import javax.ws.rs.runtimetype; import javax.ws.rs.webapplicationexception; import javax.ws.rs.client.clientrequestcontext; import javax.ws.rs.client.clientrequestfilter; import javax.ws.rs.container.prematching; import javax.ws.rs.ext.writerinterceptor; import javax.ws.rs.ext.writerinterceptorcontext; @priority(integer.min_value) @constrainedto(runtimetype.client) public class loggingfilter implements clientrequestfilter, writerinterceptor { private static final logger logger = logger.getlogger(loggingfilter.class.getname()); private static final string entity_stream_property = loggingfilter.class.getname() + ".entitylogger"; private static final charset default_charset = standardcharsets.utf_8; private static final int max_entity_size = 1024 * 8; private void log(stringbuilder sb) { logger.info(sb.tostring()); } @override public void filter(clientrequestcontext context) throws ioexception { if (context.hasentity()) { outputstream stream = new loggingstream(context.getentitystream()); context.setentitystream(stream); context.setproperty(entity_stream_property, stream); } } @override public void aroundwriteto(writerinterceptorcontext context) throws ioexception, webapplicationexception { loggingstream stream = (loggingstream) context.getproperty(entity_stream_property); context.proceed(); if (stream != null) { log(stream.getstringbuilder(default_charset)); } } private class loggingstream extends filteroutputstream { private final stringbuilder b = new stringbuilder(); private final bytearrayoutputstream baos = new bytearrayoutputstream(); loggingstream(final outputstream inner) { super(inner); } stringbuilder getstringbuilder(final charset charset) { // write entity builder final byte[] entity = baos.tobytearray(); b.append(new string(entity, 0, math.min(entity.length, max_entity_size), charset)); if (entity.length > max_entity_size) { b.append("...more..."); } b.append('\n'); return b; } @override public void write(final int i) throws ioexception { if (baos.size() <= max_entity_size) { baos.write(i); } out.write(i); } } }
and register client
:
client client = clientbuilder.newclient().register(loggingfilter.class);
Comments
Post a Comment