In this post I'm gonna show you how simple it is to enhance JAX-RS application by a new body format. In this example I use Kryo serialization framework.
Kryo serialization framework
Kryo is a fast and efficient object graph serialization framework for Java. The goals of the project are speed, efficiency, and an easy to use API. The project is useful any time objects need to be persisted, whether to a file, database, or over the network.Kryo can also perform automatic deep and shallow copying/cloning. This is direct copying from object to object, not object->bytes->object.
JAX-RS Message Body providersJAX-RS allows you to enhance JAX-RS engine to support another body type. There is MessageBodyReader and MessageBodyWriter SPI to be implemented. I have chosen to use application/x-kryo media type for Kryo (de)serialized message bodies.
You can see it is really simple Message Body provider. But I hope it shows you the way...
Than you need to register provider class into JAX-RS Application, e.g.
And finally usage of the new body type is very easy:
ConclusionWhole sources are available in dedicated GitHub repository. Use it as you need. JAX-RS provides really simple way how to extend it's support. And Kryo is really powerful framework. Give it a try. Look at deeper JVM serializers comparision.
In one of the next blog posts I will provider some performance data comparing Kryo with JSON, XML and others message body providers.
- https://github.com/EsotericSoftware/kryo - Kryo GitHub
- https://github.com/eishay/jvm-serializers - Benchmark comparing serialization libraries on the JVM
- https://github.com/jersey/jersey - Jersey GitHub
- https://jersey.java.net/ - Jersey home page
- https://jersey.java.net/documentation/latest/message-body-workers.html - Jersey Message Body providers docs
- https://github.com/shamoh/jersey-kryo - this blog post example GitHub project