Issue
I am new to MVC, and full-stack web environment in general. I am looking at a web-app's source code and trying find where are mappings between js objects and back-end java objects defined. The structure of the code looks similar this. A function in a BuildingsController is
@RequestMapping(value = "/AddNewBuilding", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<Object> AddNewBuilding(@RequestBody Building bld, HttpServletRequest request,HttpServletResponse response){
where building is define as
public class Building {
String name;
String city;
String address;
Owner owner;
And the front end (reactjs) we have populated variables name,city,etc. And they are sent as to backend.
let params = {
name: name,
city: city;
address: address,
owner: owner,
};
axios({
method: "post",
url: "./Buildings/AddNewBuilding",
data: params,
})
I cannot find the mapping anywhere in the source code between the Java object Building and the js object params. Where should I be looking, and how are they mapped in Spring?
Solution
This looks like a Spring project so it will use Jackson for (de)serialization.
The mapping is somewhat "magic" as the framework will use the variable name to match the keys in your json string. So, in your examples, name, city and address will be mapped to the corresponding variables.
Now, the Owner
is an object so it will again map the (nested) json to the variable names.
Moving forward, if the names do not match, you can annotate the Java variables. i.e. @JsonProperty
There is a whole new world of possibilities out there for mapping but the key is that, if nothing is configure, it will match on variable name by default.
Answered By - grekier
Answer Checked By - Cary Denson (JavaFixing Admin)