1

In a Spring application I have added a custom JSON serializer which is applied to a field with thte tag:

@JsonSerialize(using=MySerializer.class)

And the MySerializer serializer (whcihc extends from JsonSerializer) has a method which looks like:

@Override
public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException
{ 
    //some logic
}

It works and execute the logic when the application generates json in the methods annotated with @ResponseBody but it also executes the same logic when the application uses a webservice with JSON. I'd like to set up different configurations for the RestTemplate serialization and for @ResponseBody, or at least, being able to differenciate in the serializate method whether we are in a @ResponseBody case or in a RestTemplate one.

Any idea on how to do this?

Thanks.

Javi
  • 19,387
  • 30
  • 102
  • 135

1 Answers1

2

You should use Mix-in Annotations feature. For example, your POJO classes could look like that:

class Root {

    private Data data;

    // getters/setters
}

class Data {

    private String name;

    // getters/setters
}

Now, you have to create MixIn interface:

interface RootMixIn {

    @JsonSerialize(using = DataSerializer.class)
    Data getData();
}

Imagine, that custom serializer looks like this:

class DataSerializer extends JsonSerializer<Data> {

    @Override
    public void serialize(Data data, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
        generator.writeStartObject();
        generator.writeFieldName("name_property");
        generator.writeString(data.getName() + " XXX");
        generator.writeEndObject();
    }
}

Finally, you have to create two ObjectMapper's. Simple usage:

Data data = new Data();
data.setName("Tom");

Root root = new Root();
root.setData(data);

ObjectMapper mapperWithMixIn = new ObjectMapper();
mapperWithMixIn.addMixInAnnotations(Root.class, RootMixIn.class);

ObjectMapper mapperDefault = new ObjectMapper();

System.out.println("With MIX-IN");
System.out.println(mapperWithMixIn.writeValueAsString(root));

System.out.println("Default");
System.out.println(mapperDefault.writeValueAsString(root));

Above script prints:

With MIX-IN
{"data":{"name_property":"Tom XXX"}}
Default
{"data":{"name":"Tom"}}

As you can see, ObjectMapper with MixIn you can use in request handlers, and default ObjectMapper you can use in RestTemplate.

Update 1

Spring creates default ObjectMapper bean and uses it to serialize and deserialize request's data. You have to find and override this default bean. How to do it? Please, see below links:

This overridden bean should look like mapperWithMixIn in my above example.

Secondly, you have to create new ObjectMapper bean and inject this bean to all RestTemplate-s and use this bean in these classes as a serializer/deserializer.

Community
  • 1
  • 1
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thanks, but how can I configure a different object mapper for RestTemplate from the one used in @ResponseBody in Spring? – Javi Mar 28 '14 at 07:30
  • In your question your said that you want to set up two different configurations. Why you want to use one `ObjectMapper` to create another? Could you give me more information because I do not understand your question. – Michał Ziober Mar 28 '14 at 07:45
  • I need 2 different json formats for 2 different cases, one for Ajax requests and another for invoking Rest services. and both jsons can use the same class. That's why I need 2 different ObjectMapper configurations in the same spring application. I don't know if it's possible. – Javi Mar 28 '14 at 10:20
  • I updated my answer. I hope that now it looks much better and you will be able to find your solution. – Michał Ziober Mar 28 '14 at 23:49