简体   繁体   中英

Spring RestTemplate - Specifying Jackson View

I have a rest client class which is responsible for making calls to a rest service using a single Spring RestTemplate bean to do the HTTP exchanges. This includes serialization of MyObject.java via MappingJacksonHttpMessageConverter.java.

When creating (POST) a MyObject I send 5 fields.

When updating (PUT) a MyObject I must only send 3 (the mutable fields).

I want a single DTO, MyObject, that serializes differently under different circumstances: POST vs PUT. Step in Jackson Views which solve this problems perfectly.

My problem is that ObjectMapper is hidden away in the RestTemplate messageConverters which makes it hard to add context at REST exchange time as to which View I want the ObjectMapper to use when serializing.

Is there any way to achieve this or do I need 2 distinct RestTemplates, one configured with an Object mapper for creation and one for updating. This seems like a lot of duplication.

Thanks in advance

You can configure ObjectMapper or the DTO itself to ignore nulls during serialization. Then only set the fields you want to pass in your DTO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM