1

I am new to angularjs. I am trying to post a request from angularjs to spring controller. The request object is logged properly in angular but it reaches as null in controller.

I have been through the similar questions but they didn't worked out for me :

getting null using angular post

angularjs-spring-mvc-json-post-request

spring-angularjs-receiving-null-data-on-server

Controller.js :

$scope.addExpense = function (expense) {
        console.log("Received"+expense.amount+expense.description);

        var expenseDetail = {
            description:expense.description,
            amount:expense.amount,
            created:'',
            updated:'',
        };

        console.log("expense detail : "+JSON.stringify(expenseDetail));

        $http({
            method  : 'POST',
            dataType: 'json',
            url     : 'addExpense1',
            data    : JSON.stringify(expenseDetail),
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            console.log(response.data);
        }).error(function(response){
            console.log("Error : "+response.data);
        });
    };

Controller method :

@RequestMapping(value = "addExpense1", method = RequestMethod.POST)
@ResponseBody
public List<ExpenseDetail> addExpense1(@RequestBody ExpenseDetailDTO expenseDetailDTO) {
    LOG.info("Request to add description : " + expenseDetailDTO.getDescription());
    if(expenseDetailDTO==null || expenseDetailDTO.getDescription()==null || expenseDetailDTO.getAmount()==null)
        return null;
    expenseService.addExpense(expenseDetailDTO);

    List<ExpenseDetail> expenseDetails = expenseService.getAllForToday();
    LOG.info("Expense Details fetched : " + expenseDetails.size());
    return expenseDetails;
}

On controller following log is printed :

Request to add description : null

I am not getting error anywhere. Kindly help me out.

1
  • How u solved this..?
    – Hema
    Apr 14, 2017 at 5:25

1 Answer 1

0

i had similar issue in the past using spring webmvc.

https://github.com/sombriks/rango-vote-spring/blob/master/src/main/java/rango/vote/RangoVoteController.java

What solved in my case was the addition of jackson-databind dependency and the proper configuration:

https://github.com/sombriks/rango-vote-spring/blob/master/build.gradle#L54

https://github.com/sombriks/rango-vote-spring/blob/master/src/main/webapp/WEB-INF/applicationContext.xml#L29

If possible, prefer spring + jersey over spring webmvc since you could use jaxb to properly map your data objects:

http://pastebin.com/TVsrJJm7

http://pastebin.com/jE7z1u3k

http://pastebin.com/39QDBgQD

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.