Classes to assist serializing and deserializing protocol buffers with JAX-RS.
To serialize a Java type into a protobuf, simply have it implement either the WritesProto
or BuildsProto
interface:
interface MyType extends BuildsProto<MyTypeProto> {
@Override
default MyTypeProto toProto() { ... }
}
If using subclasses that serialize differently, you might want to consider overriding a method that returns a mutable MessageBuilder
:
interface MyType extends BuildsProto<MyTypeProto> {
String id();
@Override
default MyTypeProto toProto() {
return this.toProtoBuilder().build();
}
default MyTypeProto.Builder toProtoBuilder() {
return MyTypeProto.Builder.newBuilder().setId(this.id());
}
}
class MyClass implements MyType {
@Override
public MyTypeProto.Builder toProtoBuilder() {
return MyType.super.toProtoBuilder().setType("myclass");
}
}
Read or write types that implement the above interfaces, and annotate with the protobuf media type:
interface MyResource {
@GET
@Produces(ProtobufMediaType.APPLICATION_PROTOBUF)
MyType get(@QueryParam("id") String id);
@POST
@Consumes(ProtobufMediaType.APPLICATION_PROTOBUF)
Response post(MyType object);
}
Note that you may need to install the providers that do this manually. How you do this depends on your JAX-RS implementation - for example, if using resteasy-guice
then you simply bind
the providers.