Skip to content
Advertisement

Custom Data Binding in Spring

I have a situation here. Suppose, I’m receiving this JSON in the @RequestBody,

JavaScript

Which I’m binding to a POJO, like so.

JavaScript

Now, ResourceRefRequest, is as follows,

JavaScript

and, RefReqItem, is as follows,

JavaScript

Things are pretty well, as I expect them to be. The only problem is, refReqItem.resourceId is null, for all refs. I want to populate each of, refReqItem.resourceId, in the refs, with the one at the root; namely, with, resourceRefRequest.resourceId. Any idea, how can we achieve this?

Thanks.

Advertisement

Answer

In ResourceRefRequest, you can create a special setter for your JSON handler to use, to do your own post-processing in code:

JavaScript

When creating the Java object from JSON, this setter will be called because the method has the signature of a setter and is annotated with @JsonProperty("refs").

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