1

ヘルプ!Rest (Jersey) を介して Protocol Buffers を実装しようとしていますが、この例外が発生します。

class com.util.ProtobufMessageBodyReader
class com.util.ProtobufMessageBodyWriter
Jul 6, 2010 3:43:37 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-9102
Jul 6, 2010 3:43:37 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 45485 ms
Jul 6, 2010 3:49:00 PM org.apache.catalina.connector.CoyoteAdapter convertURI
SEVERE: Invalid URI encoding; using HTTP default
Jul 6, 2010 3:49:00 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java type, class com.example.tutorial.ProfileRequestProto$ProfileRequest, and MIME media type, application/x-protobuf, was not found

ProtobufMessageBodyReader/Writer を Apache ContextLoader にロードしました。上記のログから、Tomcat はクラスを見つけたようですが、読み取り時に失敗することは明らかです

 @Consumes("application/x-protobuf")

ここに ProtobufMessageBodyReader があります

@Provider
 @Component
 @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);
         }
     }

  @Override
  public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2) {
   // TODO Auto-generated method stub
   return false;
  }

そして、ここに ProtobufMessageBodyWriter があります

@Provider
 @Component
 @Produces("application/x-protobuf")
 public class ProtobufMessageBodyWriter implements MessageBodyWriter<Message> {

     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
         return Message.class.isAssignableFrom(type);
     }

     public long getSize(Message m, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
      return m.getSerializedSize();
     }

     public void writeTo(Message m, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders,OutputStream entityStream) throws IOException, WebApplicationException {
         entityStream.write(m.toByteArray());
     }

クライアントからのコードは次のとおりです。

URL url = new URL(URL);
   HttpURLConnection http = (HttpURLConnection)url.openConnection();
   http.setDoInput(true);
   http.setDoOutput(true);
   http.setUseCaches(false);  
         http.setRequestMethod("POST");
         http.setRequestProperty("Content-Type","application/x-protobuf");
         http.setRequestProperty("Accept", "application/x-protobuf");

         DataOutputStream stream = new DataOutputStream(http.getOutputStream ());

         if(contentType.equals("application/x-protobuf")) {
          ProfileRequest.Builder profile = ProfileRequest.newBuilder();
          profile.setName("John");
          profile.setId("123");
          profile.build().writeTo(http.getOutputStream());
         }

         stream.flush();
         stream.close();

そして、ここにサーバーからのコードがあります

 @POST
    @Consumes("application/x-protobuf")
     public byte[] processProtoRequest(ProfileRequest protoRequest) {

     byte[] result = null;     
     ProfileRequest.Builder profile = ProfileRequest.newBuilder();
     profile.mergeFrom(protoRequest);                  
     result  = getProfileProtoResponse(profile); 

        }catch(Exception e){
         }

        return result;
    }

何が問題なのかわかりません。ジャージーの設定に何かありますか? または、HTTP 経由でプロトコル リクエストを送信したときに何か問題がありますか?

どんな助けでも感謝します。

ありがとう

4

2 に答える 2

2

あなたはほとんど自分で問題を釘付けにしたと思います:

MIMEメディアタイプapplication/x-protobufが見つかりませんでした
[...]

上記のログから、Tomcatはクラスを見つけたようですが、読み取り時に失敗することは明らかです。 @Consumes("application/x-protobuf")

すぐに使用できるJersey(またはJSR-311 / JAX-RS )でサポートされているメディアタイプは、 MediaTypeクラスで定義されています。問題を解決するには、適切なメディアタイプを定義するだけで十分な場合があります。application/x-protobufスレッド[Jersey] MediaType-s?を参照してください。これに関する議論とサンプルのために。

于 2010-07-25T19:19:47.583 に答える
0

私はmaven + jersey + protobufプロジェクトで同じ問題に遭遇しました.Tomcat 6を使用しています.TomcatがProvider Classを見つけられないという問題があります。この問題を解決すると、Tomcat 6.0 は次のように表示されます。

 Deploying configuration descriptor jerseydemo2.xml
一月 26, 2015 11:13:41 上午 com.sun.jersey.api.core.WebAppResourceConfig init
信息: Scanning for root resource and provider classes in the Web app resource paths:
  /WEB-INF/lib
  /WEB-INF/classes
一月 26, 2015 11:13:41 上午 com.sun.jersey.api.core.ScanningResourceConfig logClasses
信息: Root resource classes found:
  class sample.hello.resources.AddressBookResource
一月 26, 2015 11:13:41 上午 com.sun.jersey.api.core.ScanningResourceConfig logClasses
信息: Provider classes found:
  class sample.pb.ProtobufMessageBodyReader
  class sample.pb.ProtobufMessageBodyWriter
一月 26, 2015 11:13:41 上午 com.sun.jersey.server.impl.application.WebApplicationImpl initiate
信息: Initiating Jersey application, version 'Jersey: 1.2-SNAPSHOT 01/27/2010 01:47 AM'

Provider クラスが見つからないことを示す前に。

私の問題は、web.xml ファイルが tomcat がすべてのリソースを見つけるための特定のパスを設定していることです。今、私はそれを次のように変更しました:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

それがあなたを助けることを願っています!

于 2015-01-26T19:31:43.247 に答える