I implemented the following REST call:
@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;
}
}
Also a provider class which provides and consumes 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);
}
}
}
My pom.xml looks like:
<?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>
my widgets.proto:
option java_package = "example"; option java_outer_classname = "WidgetsProtoc";
message Widget {
required string id = 1;
required string name = 2;
}
message WidgetList {
repeated Widget widget = 1;
}
For http://localhost:8080/RESTfulExample/widgets I get the following error message:
Could not find MessageBodyWriter for response object of type: hu.example.WidgetsProtoc$WidgetList of media type: application/x-protobuf
My structure: in src-main-java-example package where is the Java classes are (provider and the REST class) in src-main-proto-> the .proto class src-main-webapp-WEB-INF the web.xml file and I deployed the war file to WildFly.
I don't know why I got this message, I tried to write another method for jsos and it is works.
It seems that your annotated Provider class is unknown to JAX-RS impl.'s context which is resteasy for this case.
Since reasteasy and other JAX-RS implementations have embedded json serialization/deserialization support, your json test surely works.
According to Jboss documentation;
The JAX-RS specification allows you to plug in your own request/response body reader and writers. To do this, you annotate a class with @Provider and specify the @Produces types for a writer and @Consumes types for a reader. You must also implement a MessageBodyReader/Writer interface respectively. Here is an example.
The Resteasy ServletContextLoader will automatically scan your WEB-INF/lib and classes directories for classes annotated with @Provider or you can manually configure them in web.xml. See Installation/Configuration
First of all, add necessary logging to your @Provider class and double check if there is a nested underlying exception that causes the class from loading/initing.
Also add configuration to your web.xml for your provider class as a workaround for bypassing (if any) auto-scanning issues.
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>hu.example.ProtobufMessageWriter</param-value>
</context-param>
And, finally, a stack trace of the exception may also help if you can provide.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.