rest - MessageBodyWriter not found for media type=application/x-protobuf -
i want use protobuf in rest application. when running app, giving me following error..
messagebodywriter not found media type=application/x-protobuf
i have placed these files in same package using these. new both rest , protobuf, not getting exact problem. per many blogs mentioning @provider
or @consumer
enough jersey catch custom messagebodywriter. please me issue..
the code using follows -
string response = webtarget.path("auth").request(mediatype.text_plain) .post(entity.entity(login, "application/x-protobuf") ,string.class);
and resource -
@post @produces(mediatype.text_plain) @consumes("application/x-protobuf") public string authentication(login login) { return login.getusername().touppercase(); }
detailed log follows -
severe: servlet.service() servlet [login] in context path [/webapp] threw exception org.glassfish.jersey.message.internal.messagebodyprovidernotfoundexception: messagebodywriter not found media type=application/x-protobuf, type=class com.codeputer.lib.protolib.protos.loginproto$login, generictype=class com.codeputer.lib.protolib.protos.loginproto$login. @ org.glassfish.jersey.message.internal.writerinterceptorexecutor$terminalwriterinterceptor.aroundwriteto(writerinterceptorexecutor.java:247) @ org.glassfish.jersey.message.internal.writerinterceptorexecutor.proceed(writerinterceptorexecutor.java:162) @ org.glassfish.jersey.message.internal.messagebodyfactory.writeto(messagebodyfactory.java:1130) @ org.glassfish.jersey.client.clientrequest.writeentity(clientrequest.java:502) @ org.glassfish.jersey.client.internal.httpurlconnector._apply(httpurlconnector.java:388) @ org.glassfish.jersey.client.internal.httpurlconnector.apply(httpurlconnector.java:285) @ org.glassfish.jersey.client.clientruntime.invoke(clientruntime.java:255) @ org.glassfish.jersey.client.jerseyinvocation$2.call(jerseyinvocation.java:700) @ org.glassfish.jersey.internal.errors.process(errors.java:315) @ org.glassfish.jersey.internal.errors.process(errors.java:297) @ org.glassfish.jersey.internal.errors.process(errors.java:228) @ org.glassfish.jersey.process.internal.requestscope.runinscope(requestscope.java:444) @ org.glassfish.jersey.client.jerseyinvocation.invoke(jerseyinvocation.java:696) @ org.glassfish.jersey.client.jerseyinvocation$builder.method(jerseyinvocation.java:448) @ org.glassfish.jersey.client.jerseyinvocation$builder.post(jerseyinvocation.java:349) @ com.codeputer.webapp.requester.authenticate(requester.java:41) @ com.codeputer.webapp.login.service(login.java:49) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:170) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:98) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1041) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:607) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:315) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745)
i have following custom messagebodywriter , messagebodyreader follows -
protobufmessagebodywriter -
import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.outputstream; import java.lang.annotation.annotation; import java.lang.reflect.type; import java.util.map; import java.util.weakhashmap; import javax.ws.rs.produces; import javax.ws.rs.webapplicationexception; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.multivaluedmap; import javax.ws.rs.ext.messagebodywriter; import javax.ws.rs.ext.provider; import com.google.protobuf.message; @provider @produces("application/x-protobuf") public class protobufmessagebodywriter implements messagebodywriter<message> { /** * cache save cost of duplicated call(getsize, writeto) 1 * object. */ public boolean iswriteable(class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { return message.class.isassignablefrom(type); } private map<object, byte[]> buffer = new weakhashmap<object, byte[]>(); public long getsize(message m, class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { bytearrayoutputstream baos = new bytearrayoutputstream(); try { m.writeto(baos); } catch (ioexception e) { return -1; } byte[] bytes = baos.tobytearray(); buffer.put(m, bytes); return bytes.length; } public void writeto(message m, class type, type generictype, annotation[] annotations, mediatype mediatype, multivaluedmap httpheaders, outputstream entitystream) throws ioexception, webapplicationexception { entitystream.write(buffer.remove(m)); } }
protobufmessagebodyreader -
import java.io.ioexception; import java.io.inputstream; import java.lang.annotation.annotation; import java.lang.reflect.method; import java.lang.reflect.type; import javax.ws.rs.consumes; import javax.ws.rs.webapplicationexception; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.multivaluedmap; import javax.ws.rs.ext.messagebodyreader; import javax.ws.rs.ext.provider; import com.google.protobuf.generatedmessage; import com.google.protobuf.message; @provider @consumes("application/x-protobuf") public class protobufmessagebodyreader implements messagebodyreader<message> { public boolean isreadable(class<?> type, type generictype, annotation[] annotations, mediatype mediatype) { return message.class.isassignablefrom(type); } public message readfrom(class<message> type, type generictype, annotation[] annotations, mediatype mediatype, multivaluedmap<string, string> httpheaders, inputstream entitystream) throws ioexception, webapplicationexception { try { method newbuilder = type.getmethod("newbuilder"); generatedmessage.builder builder = (generatedmessage.builder) newbuilder.invoke(type); return builder.mergefrom(entitystream).build(); } catch (exception e) { throw new webapplicationexception(e); } } }
Comments
Post a Comment