java - Jersey REST Client - Treat Custom MediaType as MediaType.APPLICATION_JSON -
i'm writing rest client using jersey with jacksonfeature enabled webservice forces me specify custom-named content type, though it's regular json. in other words, when this:
request request = buildmysamplerequestpojo(); response response = requestbuilder.post( entity.entity(request, mediatype.application_json) );
the service complains i'm using invalid content type. can around specifying custom-named media type in place of mediatype.application_json constant:
response response = requestbuilder.post( entity.entity(request, "vnd.stupidnamethatreallyisjustjson+json") );
however, when that, get:
*severe: messagebodywriter not found media type=stupidnamethatreallyisjustjson*
is there way can have jersey treat customized media type name if regular json without writing customized messagebodywriter?
i think use both this (jax-rs entity providers) , this (use jackson jersey), in order achieve want. in nutshell, register messagebodywriter
annotated @produces("application/vnd.stupidnamethatreallyisjustjson+json")
, in implementation delegate marshalling/unmarshalling jackson.
edit : try along lines of
package my.pack.age; import com.sun.jersey.core.provider.abstractmessagereaderwriterprovider; import com.sun.jersey.core.util.readerwriter; import javax.ws.rs.produces; import javax.ws.rs.webapplicationexception; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.multivaluedmap; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.outputstreamwriter; import java.lang.annotation.annotation; import java.lang.reflect.type; @produces("application/vnd.stupidnamethatreallyisjustjson+json") public class myjsonbodywriter<t> extends abstractmessagereaderwriterprovider<t> { // t should pojo in case. if can make pojo compatible org.codehaus.jettison.json.jsonobject, // can extend com.sun.jersey.json.impl.provider.entity.jsonobjectprovider , delegate methods of // messagebodywriter (and messagebodyreader) that. otherwise implement them. @override public t readfrom(class<t> type, type generictype, annotation annotations[], mediatype mediatype,multivaluedmap<string, string> httpheaders, inputstream entitystream) throws ioexception { try { // deserialize entitystream according given media type , return new instance of t. // // can (just example) : // jsonobject myobject = new jsonobject(); // try { // these names , values should come stream. // myobject.put("name", "agamemnon"); // myobject.put("age", 32); // } catch (jsonexception ex) { // logger.log(level.severe, "error ...", ex); // } return null; } catch (exception e) { throw new webapplicationexception(new exception("error during deserialization", e),400); } } @override public void writeto(t t,class<?> type, type generictype, annotation annotations[], mediatype mediatype, multivaluedmap<string, object> httpheaders, outputstream entitystream) throws ioexception { try { outputstreamwriter writer = new outputstreamwriter(entitystream, readerwriter.getcharset(mediatype)); // write t on writer writer.flush(); } catch (exception e) { throw new webapplicationexception( new exception("error during serialization", e), 500); } } @override public boolean iswriteable(class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { // should return true if class can serialize given type given media type return true; } @override public boolean isreadable(class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { // should return true if class can deserialize given type given media type return true; } @override public long getsize(t t, class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { // calculate , return content-lenght, i.e. lenght of serialized representation of t return 0; } }
obviously starting point, not working example, should give enough information start. remember you'll have register class jersey in order let use it.
Comments
Post a Comment