In my spring cloud function application I’ve created a custom message converter.
I am converting from JSON to HapiFhir Bundle.class
and then from the Bundle.class
back to JSON.
The convertFromInternal
method works as expected. It successfully converts the JSON to the Bundle.class
However, convertToInternal
never gets invoked…
I debugged the code and found that the SimpleFunctionRegistry.class
calls a method convertOutPutIfNecessary
… this class requires String[] contentType
The contentType
is not set and subsequently does not trigger my custom convertToInternal
method.
The Bundle.class
is just .toString()
as the output message. This is converted to JSON but not the correct JSON formatted message, hence why I created custom message converter.
I cannot determine how or where the contentType
value is set in the SimpleFunctionRegistry.class
Am I using the CustomMessageConverter
Correctly?
Below is my CustomMessageConverter class
public MyCustomMessageConverter() {
super(new MimeType("application", "json+fhir"), new MimeType("application", "json"));
}
@Override
protected boolean supports(Class<?> clazz) {
return (Bundle.class.equals(clazz));
}
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
Object payload = message.getPayload();
FhirContext ctx = FhirContext.forR4();
return ctx.newJsonParser().parseResource(Bundle.class, (String) payload);
}
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
FhirContext ctx = FhirContext.forR4();
return ctx.newJsonParser().encodeResourceToString((Bundle) payload);
}
}
The contentType
typically comes as Message header. For example MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "application/json")
.
They can also come as headers/properties from inbound messages (Kafka, Rabbit etc) and then we internally convert it to Message header as explained above.
In any event, if you still have question or struggling with it, feel free to raise an issue in https://github.com/spring-cloud/spring-cloud-function and include a sample that reproduces it so we can take a look. You can either push your sample to GitHub and include a link or attach zip file