1 Haziran 2018 Cuma

How to write additional custom mapping in serialization or deserialization of Jackson in Spring-Boot application

I develop a Spring boot project (Spring 4.0) as a middleware application (MW) between client and backend sides. I use jackson-datatype-jsr310 to send out JSON results for messaging with client and backend sides.
MW creates java.util.Currency objects by using currency code information on backend response. However, I want to map a currency code to custom currency code while sending the client-side. For example: MW gets the currency code - "TRY" from backend and Currency object is created. But, I want it to be sent as "TL" to the client-side.
First, I think that two custom object mappers should be written; one is for RestTemplate, I mean for messaging between backend and MW and second is for default Spring object mapper. This default object mapper would be also used for messaging between client and MW. Furthermore, jackson configurations are kept in application.properties should be moved to these object mappers separately. Management of these configurations should be managed through on these two object mapper files.
However, I write the class with annotation - JsonComponent below:
@JsonComponent
public class CurrencyCombinedSerializer {

    public static class CurrencySerializer extends JsonSerializer<Currency> {

        @Override
        public void serialize(final Currency currency, final JsonGenerator jgen, final SerializerProvider provider)
            throws IOException {

            if (Constant.TRY.equals(currency.getCurrencyCode())) {
                jgen.writeString(Constant.TL);
            } else {
                jgen.writeString(currency.getCurrencyCode());
            }
        }
    }

    public static class CurrencyDeserializer extends JsonDeserializer<Currency> {

        @Override
        public Currency deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
            final JsonNode node = jp.getCodec().readTree(jp);

            final String currencyCode = node.asText();

            if (Constant.TL.equals(currencyCode)) {
                return Currency.getInstance(Constant.TRY);
            }

            return Currency.getInstance(currencyCode);
        }
    }
}
This class adds Currency related serializer and deserializer into default Spring object mapper. It starts to make currency code mapping between client-side and MW. 

The suprised thing is messaging between backend and MW is not changed. I mean currency codes still are serialized and deserialized as is. Does RestTemplate use its object mapper? I don't know. But this is what I need. The code above is solved my problem.

References:
  1. http://www.baeldung.com/spring-boot-jsoncomponent
  2. http://www.baeldung.com/jackson-deserialization
  3. http://www.baeldung.com/jackson-custom-serialization
  4. http://gdpotter.com/2017/05/24/custom-spring-mvc-jackson/
  5. https://stackoverflow.com/questions/11319445/java-to-jackson-json-serialization-money-fields
  6. https://stackoverflow.com/questions/28324352/how-to-customise-the-jackson-json-mapper-implicitly-used-by-spring-boot?rq=1