Replace JacksonSerializerFactory as default SerializerFactory

Hey,

My project is primarily using Protobuf based requests and responses, and I’d like to replace the JacksonSerializerFactory entirely as the default factory for my services…

Ideally I’d like the ability to fall back to the JacksonSerializerFactory for messages that aren’t of my protobuf types.

So this comes down to 2 questions, first is it possible to change the default universally to a new factory, and second is it possible to chain factories, trying a second when the first fails and so on.

The ideal seems to be to somehow replace the Guice bindings to the Jackson factory, but the Guice factory injection request seems hardcoded to Jackson in most places.

As an aside, an option to specify the factory class instead of the factory instance in the descriptor seems like it should allow injecting the replacement factory and allow better configurability for custom factories.

Any thoughts, suggestions or advice are welcome.

Thanks!

The serializer is something that needs to be agreed on by the client and the server. This is why it is defined in the service descriptor rather than having a default defined elsewhere.

As an example, if you were able to change the default serializer factory for a service to protocol buffers, how would a client know to use it instead of Jackson? What if one service is a client of two other services, one that uses protocol buffers and another that uses JSON? This is why the serializer factory is specified on the service descriptor: it’s a design principle of Lagom that all of the information that the client and the service need to agree on to communicate should be defined in the descriptor.

To set the default serializer factory for a service descriptor, you can call withSerializerFactory on it and pass the serializer factory instance you want to use. This can be overridden with per-type and per-call serializers that fall back to the default. In your situation, where you have some types with protobuf serializers and others that use Jackson, the best approach would be to define per-type serializers for the ones that do use protobuf, and keep using the built-in Jackson serializer factory as the default for the service.

There are more details in the documentation on How Lagom selects a message serializer.

As an aside, an option to specify the factory class instead of the factory instance in the descriptor seems like it should allow injecting the replacement factory and allow better configurability for custom factories.

This would absolutely be a nice improvement. There is an open issue to add this support, but it hasn’t been implemented yet Support dependency injection for objects used by service descriptors · Issue #1072 · lagom/lagom · GitHub

Aha, I should have searched the issues database first I suppose.

Thank you, it looks like maybe I can look forward to that in the future.