Reputation: 77
I am following this tutorial: https://spring.io/guides/gs/consuming-rest/ It is consuming a JSON object, like this one:
{
type: "success",
value: {
id: 10,
quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
}
}
I have one question. In Value.java, we have two variables:
private Long id;
private String quote;
My question is how does Spring know to bind the variable id to the id property in JSON and how does it know to bind quote variable to the quote property in JSON. I tried making both String thinking that maybe Spring auto determines the data-type of a variable and then does the binding but that didn't make a difference. I thought maybe if the variable names are same as the property, that's how it does the binding, so I tried changing the variable names and that didn't make a difference either. Then I thought it is maybe the order of the variables so I switched the variables so it became like this:
private String quote;
private String id;
I made them both String on purpose. But still somehow the id property was getting binded to the variable id and quote property to quote variable.
So could someone tell me like how does Spring determine which property to bind to which variable.
Upvotes: 0
Views: 121
Reputation: 121
Since it's Jackson, default behaviour is to use coresponding getters/setters so my shot is that you changed field name but not getter/setter name.
Upvotes: 1