Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么会出现NoMessageBodyWriterFoundFailure错误?_Java_Protocol Buffers - Fatal编程技术网

Java 为什么会出现NoMessageBodyWriterFoundFailure错误?

Java 为什么会出现NoMessageBodyWriterFoundFailure错误?,java,protocol-buffers,Java,Protocol Buffers,我实现了以下REST调用: @Path("/widgets") public class WidgetResource { @GET @Produces("application/x-protobuf") public WidgetsProtoc.WidgetList getAllWidgets() { Widget widget1 = Widget.newBuilder().setId("1").setName("widget

我实现了以下REST调用:

@Path("/widgets")
public class WidgetResource {

    @GET
    @Produces("application/x-protobuf")
    public WidgetsProtoc.WidgetList getAllWidgets() {
        Widget widget1 =
            Widget.newBuilder().setId("1").setName("widget 1").build();
        Widget widget2 =
            Widget.newBuilder().setId("2").setName("widget 2").build();
        WidgetsProtoc.WidgetList list = WidgetsProtoc.WidgetList.newBuilder().addWidget(widget1).addWidget(widget2).build();
        return list;
   }
}
也是一个提供和使用application/x-protobuf的提供者类:

@Provider
@Produces("application/x-protobuf")
@Consumes("application/x-protobuf")
public class ProtobufMessageWriter implements MessageBodyWriter<WidgetsProtoc.WidgetList>, MessageBodyReader<WidgetsProtoc.WidgetList> {

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

    @Override
    public long getSize(WidgetsProtoc.WidgetList widgetList, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
       return widgetList.getSerializedSize();
    }

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

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

   @Override
   public WidgetsProtoc.WidgetList readFrom(Class<WidgetsProtoc.WidgetList> 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 (WidgetsProtoc.WidgetList) builder.mergeFrom(entityStream).build();
      } catch (Exception e) {
        throw new WebApplicationException(e);
     }
   }
 }
对于,我收到以下错误消息:

找不到类型为的响应对象的MessageBodyWriter: hu.example.WidgetsProtoc$WidgetList媒体类型: 应用程序/x-protobuf

我的结构: 在src主java示例包中,java类在哪里(provider和REST类) 在src main proto->the.proto类中 src main webapp WEB INF WEB.xml文件 我把war文件部署到WildFly上


我不知道为什么我得到了这个消息,我试图为jsos编写另一个方法,它是有效的。

似乎您的带注释的提供者类对于JAX-RS impl.的上下文来说是未知的,对于这种情况来说,它很容易

由于reasteasy和其他JAX-RS实现具有嵌入式json序列化/反序列化支持,因此您的json测试肯定可以工作

根据Jboss文档

JAX-RS规范允许您插入自己的请求/响应主体读写器。为此,使用@Provider注释类,并为编写器指定@products类型,为读取器指定@Consumes类型。您还必须分别实现MessageBodyReader/Writer接口。这里有一个例子

Resteasy ServletContextLoader将自动扫描WEB-INF/lib和classes目录,查找带有@Provider注释的类,或者您可以在WEB.xml中手动配置它们。请参阅安装/配置

首先,向@Provider类添加必要的日志记录,并仔细检查是否存在导致类加载/初始化的嵌套底层异常

还可以为提供程序类将配置添加到web.xml中,作为绕过(如果有)自动扫描问题的解决方法

<context-param>
 <param-name>resteasy.providers</param-name>
 <param-value>hu.example.ProtobufMessageWriter</param-value>
</context-param>

resteasy.providers
hu.example.ProtobufMessageWriter

最后,如果您能够提供,异常的堆栈跟踪可能也会有所帮助。

您的带注释的提供程序类对于JAX-RS impl.的上下文来说似乎是未知的,这在本例中非常容易

由于reasteasy和其他JAX-RS实现具有嵌入式json序列化/反序列化支持,因此您的json测试肯定可以工作

根据Jboss文档

JAX-RS规范允许您插入自己的请求/响应主体读写器。为此,使用@Provider注释类,并为编写器指定@products类型,为读取器指定@Consumes类型。您还必须分别实现MessageBodyReader/Writer接口。这里有一个例子

Resteasy ServletContextLoader将自动扫描WEB-INF/lib和classes目录,查找带有@Provider注释的类,或者您可以在WEB.xml中手动配置它们。请参阅安装/配置

首先,向@Provider类添加必要的日志记录,并仔细检查是否存在导致类加载/初始化的嵌套底层异常

还可以为提供程序类将配置添加到web.xml中,作为绕过(如果有)自动扫描问题的解决方法

<context-param>
 <param-name>resteasy.providers</param-name>
 <param-value>hu.example.ProtobufMessageWriter</param-value>
</context-param>

resteasy.providers
hu.example.ProtobufMessageWriter
最后,如果您能够提供,异常的堆栈跟踪也可能会有所帮助

<context-param>
 <param-name>resteasy.providers</param-name>
 <param-value>hu.example.ProtobufMessageWriter</param-value>
</context-param>