Skip to content
Advertisement

Spring RestTemplate: how to send the payload body as compact form so without rn

I am dealing with an API that don’t accept multi line json body and accept only 1 json line body (json compact form)

The below payload is valid because it’s compact in just 1 line working

And the below payload is not passing because it’s multiline

Not working

I have the same problem in the Java/Spring code where I got this error while posting my object in restemplate.

Is there a way to convert the payload body into 1 single json line?

Code I am using to post the payload via RestTemplate

JavaScript

Advertisement

Answer

The solution that worked for me is to annotate my request class with a custom JsonSerializer

This MyRequestClassSerializer#serialize will be called once restTemplate.exchange(uri, HttpMethod.POST, entity , type); is executed

Hence the payload will be compacted in 1 line by using JsonGenerator#writeRawValue

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement