Please note: Although this question specifically mentions Dropwizard, I believe anyone with Jersey/JAX-RS experience should be able to answer this question, as I would imagine Dropwizard is just following Jersey/JAX-RS conventions under the hood.
I have a Dropwizard service that reds/writes in JSON and works beautifully.
I would like to now switch it to read/write binary data (to minimize network bandidth). I see there is the Dropwizard-Protobuf lib but I have a few concerns about implementing binary serialization in Dropwizard.
First off, here's the important stuff from my current (JSON-centric) code:
// Groovy pseudo-code
// Domain entity/POJO
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
class Fizz {
@JsonProperty
String name
@JsonProperty
boolean isBuzz
}
// The Dropwizard app entry point
class FizzService extends Application<FizzConfiguration> {
@Override
void run(FizzConfiguration fizzCfg, Environment env) throws Exception {
// ... lots of stuff
env.jersey().register(new FizzService())
}
}
// JAX-RS resource with a sample GET endpoint
@Path(value = "/fizz")
@Produces(MediaType.APPLICATION_JSON)
class FizzResource {
@GET
@Path("/{id}")
Fizz getFizzById(@PathParam("id") int id) {
// Look up a 'Fizz' in a DB and return it.
lookupFizzinDB(id)
}
}
So as you can see, the GET /fizz
endpoint expect a JSON request entity that contains an element called id
of type int
. It returns a Fizz
response entity that matches the provided id
.
I want to switch this from JSON to binary via Google Protocol Buffers.
According to the Dropwizard-Protobuf docs, this is as simple as just adding this to my FizzService#run(...)
method:
environment.jersey().register(new ProtocolBufferMessageBodyProvider())
The problem is that currently my whole app is wired to serialize/deserialize to/from JSON. The @JsonProperty
annotations on my Fizz
class have meaning to Dropwizard. The @Produces(MediaType.APPLICATION_JSON)
annotation on the FizzResource
also plays a critical role. I'm worried that making my Dropwizard app read/write protobuf-generated binary is not as easy as the 1-liner posted in the docs.
I'm not married to this library. If anyone has any experience setting up REST endpoints in a Dropwizard app to accept/receive protobuf-generated binary, all I care about is a working, efficient solution. Ideas?