【问题标题】:Why do I get NoMessageBodyWriterFoundFailure error?为什么我会收到 NoMessageBodyWriterFoundFailure 错误?
【发布时间】:2017-01-04 08:50:51
【问题描述】:

我实现了以下 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);
     }
   }
 }

我的 pom.xml 看起来像:

 <?xml version="1.0"?>
 <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>RESTfulExample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
    <repository>
        <id>JBoss repository</id>
        <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jettison-provider</artifactId>
        <version>3.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.15</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19.3</version>
    </dependency>
</dependencies>
<pluginRepositories>
    <pluginRepository>
        <id>protoc-plugin</id>
        <url>https://github.com/xolstice/protobuf-maven-plugin</url>
    </pluginRepository>
</pluginRepositories>

<build>
    <finalName>RESTfulExample</finalName>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.0</version>
            <configuration>
                <protocExecutable>C:\Users\DevUser\Downloads\protoc-3.1.0-win32\bin\protoc</protocExecutable>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

我的 widgets.proto:

选项 java_package = "示例"; option java_outer_classname = "WidgetsProtoc";

message Widget {
required string id = 1;
required string name = 2;
}

message WidgetList {
repeated Widget widget = 1;
}

对于http://localhost:8080/RESTfulExample/widgets,我收到以下错误消息:

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

我的结构: 在 src-main-java-example 包中,Java 类在哪里(提供程序和 REST 类) 在 src-main-proto-> .proto 类中 src-main-webapp-WEB-INF web.xml 文件 我将战争文件部署到 WildFly。

我不知道为什么会收到此消息,我尝试为 jsos 编写另一种方法并且它有效。

【问题讨论】:

    标签: java protocol-buffers


    【解决方案1】:

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

    由于 reasteasy 和其他 JAX-RS 实现已嵌入 json 序列化/反序列化支持,因此您的 json 测试肯定有效。

    根据 Jboss 文档;

    JAX-RS 规范允许您插入自己的请求/响应正文阅读器和编写器。为此,您使用@Provider 注释一个类,并为编写器指定@Produces 类型,为阅读器指定@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>
    

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

    【讨论】: