0

I'm writing a Spring boot application that uses RestController to expose one resource. The only resource, MainResource, is something like this:

String id; String title; List<String> tags; ComplexGraphObject gObject;

To make this work I wrote DTOs for the MainResource and for gObject and its fields where necessary. I annotated the DTOs with Jackson and wrote DTO-Mappers that return domain models. Spring correctly serializes the requests to the DTOs and an application service calls the DTO-Mappers for domain objects.

I've now realized that the gObject should be its own library, as we'd like to use it for client and server applications.

My objectives are:

  1. The library should take serialized gObjects in json/xml and return domain objects.
  2. The library should take domain gObjects and return serialized json/xml
  3. Enable applications that already use Jackson to work seamlessly as possible with the library like Spring Boot for example
  4. Provide convenience methods for other applications that might use something like gson for example

From what I've read a custom jackson de/serializer is the way to go. But between custom serializers, ObjectMapper, MixIns, etc I don't know how to do it.

EDIT: I think the mixins purpose is kinda what my DTOs are doing, and the DTO-Mapper is kinda doing what the custom de/serializer should. Is there a way to mix the two?

EDIT: Important part I forgot. The DTO-Mapper generates domain objects from the DTO as well as a matrix that represents the needed relationships from each node in the graph so they can be connected accordingly. So I guess I'd need to do that in the custom serializer?

jam01
  • 124
  • 7

1 Answers1

1

If you are just using only annotations on your DTO classes, any other spring project can serialize/deserialize those DTOs without additional configuration. For me, this is the preferred way to customize serialization.

If you change the configuration of the ObjectMapper, users of your library need to do the same. You can implement a jackson Module :

@Component
public class DateTimeModule extends SimpleModule {

public DateTimeModule() {
    super(DateTimeModule.class.getName(), Version.unknownVersion());
    //do whatever you need here....
    addSerializer(DateTime.class, new DateTimeSerializer());
    addDeserializer( DateTime.class, new DateTimeDeserializer(DateTime.class));
}
}

User of your library must register the module to the ObjectMapper with

  • ObjectMapper#findAndRegisterModules
  • ObjectMapper#registerModules

There are different ways to do this in spring, see this post

Jackson Mixins allow to use serialization annotations for classes, you cannot modify. If you are using Mixins, you need to configure them in the Module.

If you are using custom serializer, you need to configure them in the module, as shown in the example.

Serializing the graph sounds like a use case for a custom serializer.

Community
  • 1
  • 1
  • Thanks for the answer. I think this is the direction I'm headed, so you're reinforcing my approach. The problem I see looming is generating XML, as far as I can tell annotations are the only way of setting things like attributes. I suppose I will need some mixins somewhere... – jam01 Apr 09 '17 at 08:59