I am trying to send json objectfrom javascript to the Spring controller. I am using angularJs $http post for that. I am getting the lastName as null when I send the key as obejct. However, when I send the string as hard-coded value, the value shows up in the controller. Here is the controller code:
@RequestMapping(value="/edit", method= RequestMethod.POST)
@ResponseBody
public void editInformation(@RequestBody UserDetails userDetails){
LOGGER.debug("THE LASTNAME IS: "+userDetails.getLastName());
//codes.....
}
Here is the angularJs code:
$fieldProperty =$(this).attr("name");
$inputValue =$(this).val();
$http.post("/app/edit", {$fieldProperty : $inputValue}).success(function(result){
alert("Success "+result)
}).error(function(data, status){
//$log.info("The error is: "+data+ " and the error status code is: "+status)
alert("failure"+" and the data is: "+data+ " and the stis "+status)
});
Sending {$fieldProperty : $inputValue} as JSON returns userDetails.getLastName() as null. However, sending {"lastName" : $inputValue} returns the proper value. I checked with alert($fieldProperty) and it returns lastName though.
I am using Google Gson library.
Is there something I am missing here. I would appreciate your help. Thank you.