1

I need to make an API call out with a JSON object. This has worked for me using data classes and using the restTemplate.postForEntity(). The issue I'm running into is that the API is looking for a property of { "object": ... } and I can't create a val in the data class of object because it is a reserved keyword.

I tried to override the toString method to output "RequestClass(\"object\"=$obj)" but that didn't work. Is there another class that needs to be overridden with the restTemplate, or is there a different way to create a property with the same name as a reserved keyword?

DuckyDisciple
  • 105
  • 1
  • 1
  • 11

2 Answers2

1

That link helped fix it. Naming the variable `object` allowed it to keep that naming scheme. Thanks!

DuckyDisciple
  • 105
  • 1
  • 1
  • 11
0

I'm not a big fan of using the backticks, because it pollutes your code. You end up having to use those backticks everywhere you want to reference that field. You could name it something appropriate that's not a reserved keyword, and use the mapping layer (presumably jackson) to change the serialized name:

@field:JsonProperty("object")

I tend to have to do this from time to time for json fields with the name "default" and such.

Matt
  • 11,523
  • 2
  • 23
  • 33