I am trying to serialize a message (then deserialize it) and I do not want any of the headers json__TypeId__ or json_resolvableType to contain the canonical name of the class. This is because I am sending the message over the network and I consider including the canonical name in the header a security concern.
Here is just the relevant parts of the code that I am using:
package com.test; @Getter @ToString class CustomObject { String field; }
Serializing it with:
Message<?> serialized = Transformers.toJson().transform(MessageBuilder.withPayload(new CustomObject()).build());
The serialized message looks like this:
GenericMessage [payload={"field":null}, headers={id=496c110d-fdde-b03b-42c0-21f9671e29ed, json_resolvableType=com.test.CustomObject, json__TypeId__=class com.test.CustomObject, contentType=application/json, timestamp=1623417277287}]
I would prefer to have it like below, containing only the class name:
GenericMessage [payload={"field":null}, headers={id=496c110d-fdde-b03b-42c0-21f9671e29ed, json__TypeId__=CustomObject, contentType=application/json, timestamp=1623417277287}]
or even without the json__TypeId__ header at all:
GenericMessage [payload={"field":null}, headers={id=496c110d-fdde-b03b-42c0-21f9671e29ed, contentType=application/json, timestamp=1623417277287}]
I have a temporary solution, but I want to know if there is a more elegant one, like an annotation or configuration. Below is the current programatic way of solving this.
Make a class that extends the jackson mapper and the override the populateJavaTypes (which was adding the headers in the first place)
class JsonObjectMapperCustom extends Jackson2JsonObjectMapper { @Override public void populateJavaTypes(Map<String, Object> map, Object object) { } }
And then serialize with:
Message<?> serialized = new ObjectToJsonTransformer(new JsonObjectMapperCustom()).transform(message);
Note: I am using spring-integration-core 5.2.3.RELEASE
Advertisement
Answer
You can create a new message from transformed and remove headers you don’t need
Message<?> serialized = MessageBuilder.fromMessage(Transformers.toJson() .transform(MessageBuilder .withPayload(new CustomObject()) .build())) .removeHeaders("json_resolvableType", "json__TypeId__") .build();