I am working on a Kotlin SpringBoot application.
I need to send a special character (Euro symbol for example) in the request. If I send the unicode code point then it is parsed and displayed correctly by the third party system.
I could do that with Postman tool but not able to do the same from my app.
Postman example:
{
"comment": "The value is € 200 and \u20ac 300"
}
Third party system output:
{
"comment": "The value is € 200 and \u20ac 300"
}
Here we can see that \u20ac
is sent correctly from Postman to the third party system.
My application code:
Controller:
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class TestRestController(
private val restClient: RestClient
) {
data class Data(
val comment: String
)
@PostMapping("/data-test")
fun dataTest(): ResponseEntity<String> {
val euroSymbol = "\u20ac"
val escapedEuroSymbol = "\\u20ac"
val mockData = Data(
comment = "The value is $euroSymbol 200 and $escapedEuroSymbol 300"
)
restClient.createRequest(mockData)
return ResponseEntity.status(HttpStatus.OK).body("ok")
}
}
Rest client:
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import javax.validation.Valid
@FeignClient(
value = "restClient",
url = "https://envpqi8gjjxx8.x.pipedream.net/",
)
interface RestClient {
@PostMapping(
value = ["/test"],
consumes = [MediaType.APPLICATION_JSON_VALUE]
)
fun createRequest(
@RequestBody request: @Valid TestRestController.Data
): String?
}
My object mapper config:
objectMapper.setTimeZone(TimeZone.getDefault())
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
objectMapper.registerModules(kotlinModule(), JavaTimeModule())
Third party system output:
{
"comment": "The value is € 200 and \\u20ac 300"
}
Now in the mockData variable you can see that I am using both \u20ac
and \\u20ac
. for the first one the value is corrupt and the second one is sent as \\u20ac
. I want to be able to send \u20ac
as plain text similar to how I was able to do it using Postman.
How to do that?