找不到媒体类型= application / x-protobuf的MessageBodyWriter

时间:2015-11-01 13:53:48

标签: rest protocol-buffers

我想在我的REST应用程序中使用protobuf。但是当我运行应用程序时,它会给我以下错误..

MessageBodyWriter not found for media type=application/x-protobuf

我已将这些文件放在我使用这些文件的同一个包中。我是REST和protobuf的新手,所以我没有得到确切的问题。根据许多博客,仅提及@Provider@Consumer就足以让球衣捕捉到自定义的MessageBodyWriter。请帮我解决这个问题..

我使用它的代码如下 -

String response = webTarget.path("auth").request(MediaType.TEXT_PLAIN)
                    .post(Entity.entity(login, "application/x-protobuf")
                                    ,String.class);

,资源是 -

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes("application/x-protobuf")
    public String authentication(Login login) {
        return login.getUsername().toUpperCase();
    }

详细日志如下 -

SEVERE: Servlet.service() for servlet [login] in context with path [/webapp] threw exception
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/x-protobuf, type=class com.codeputer.lib.ProtoLib.Protos.LoginProto$Login, genericType=class com.codeputer.lib.ProtoLib.Protos.LoginProto$Login.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:502)
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388)
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)
    at com.codeputer.webapp.Requester.authenticate(Requester.java:41)
    at com.codeputer.webapp.login.service(login.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我有以下自定义MessageBodyWriter和MessageBodyReader,如下所示 -

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> {  
    /** 
     * a cache to save the cost of duplicated call(getSize, writeTo) to one 
     * 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);  
        }  
    }  
}  

0 个答案:

没有答案