I'm creating a POST request to an API, and one of the values in the body is a GString (Groovy String).
final body = [
'webhook': [
'topic' : topic,
'address': "${webhookConfig.address}",
'format' : 'json'
]
]
entity = new RequestEntity(body, headers, HttpMethod.POST, webhooksUri)
final createResponse = springRestTemplate.exchange(entity, Map)
I'm receiving an error from the API because the value isn't a String, but an Object.
If I change the address
value line to be:
'address': "${webhookConfig.address}".toString(),
It works fine.
I found this gist that creates a custom Jackson serializer, and I see that it's picked up in my Spring context, but it isn't used when RestTemplate serializes the data.
@Component
class GStringJsonSerializer extends StdSerializer<GString> {
@Autowired
GStringJsonSerializer(ObjectMapper objectMapper) {
super(GString)
def module = new SimpleModule()
module.addSerializer(GString, this)
objectMapper.registerModule(module)
}
@Override
void serialize(GString value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeString(value.toString())
}
}
How can I tell RestTemplate to convert Groovy's GString's into just Strings?