协议缓冲区MIME问题 [英] Protocol Buffers MIME problem
问题描述
帮助!
尝试通过Rest(Jersey)实现Protocol Buffers,但得到此异常。
Help! Trying to implement Protocol Buffers via Rest (Jersey), but get this exception.
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 Apache ContextLoader中的/ Writer。
从上面的日志中,似乎Tomcat找到了这个类,但它显然在读取时失败了
I loaded ProtobufMessageBodyReader/Writer in the Apache ContextLoader. From the log above, it seems Tomcat found the class but it apparent it fails when it reads
@Consumes("application/x-protobuf")
这是ProtobufMessageBodyReader
Here is 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
And here is 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());
}
以下是来自客户的代码:
Here is the code from client :
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;
}
我无法弄清问题是什么。
Jersey配置有什么吗?或者当我通过HTTP发送协议请求时出了什么问题?
I cannot figure out what is the problem. Is there anything with Jersey config? Or something is wrong when I sent protocol request via HTTP?
任何帮助都会很感激。
谢谢
推荐答案
你自己已经考虑过这个问题了:
You mostly nailed the issue yourself already I think:
和MIME媒体类型,application / x-protobuf,未找到
[...]
and MIME media type, application/x-protobuf, was not found
[...]
从上面的日志中看来,似乎Tomcat找到了这个类,但是当它读取
@Consumes(application / x-protobuf)
From the log above, it seems Tomcat found the class but it apparent it fails when it reads
@Consumes("application/x-protobuf")
泽西岛(或者更确切地说是 JSR-311 / JAX-RS )开箱即用的定义在类MediaType 。要解决您的问题,为 application / x-protobuf
定义适当的媒体类型可能就足够了,请参阅thread [Jersey] MediaType-s?有关此问题的讨论和示例。
The media types supported by Jersey (or rather JSR-311/JAX-RS) out of the box are defined in class MediaType. To resolve your issue it might be enough to define an appropriate media type for application/x-protobuf
, see thread [Jersey] MediaType-s? for a discussion and samples regarding this.
这篇关于协议缓冲区MIME问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!