2

I am getting 400 (Bad Request) error when I submit the form. I am using Jackson API jackson-core-asl-1.9.10.jar and jackson-mapper-asl-1.9.10.jar.

I am able to receive JSON but unable to submit it.

myscript.js

var app = angular.module('project', ['ngRoute']);
app.controller('projectFormCtrl',["$scope", "$http", function($scope, $http) {

    $scope.submitProject = function() {
        console.log($scope.project); //Works fine
        $http.post('saveProject',$scope.project).success(function () {
            console.log($scope.project);
        });
    };

}]);

WebController.java

@RequestMapping(value="/saveProject", method=RequestMethod.POST)
public @ResponseBody ProjectDetails submitProject(@RequestBody ProjectDetails projectDetails) {
    System.out.println(projectDetails);
    return projectDetails;
}
rohitpal
  • 455
  • 1
  • 6
  • 21

2 Answers2

1

I have also gone through the same problem of Bad request. I resolved it by doing the following code.
You can post an array to a controller by converting it into json string by JSON.stringify(array).
I have pushed muliple Objects into an array using push().

    var a = [];
    for(var i = 1; i<10; i++){
        var obj = new Object();
        obj.name = $("#firstName_"+i).val();
        obj.surname = $("#lastName_"+i).val();
        a.push(obj);
    }

    var myarray = JSON.stringify(a);
    $.post("/ems-web/saveCust/savecustomers",{myarray : myarray},function(e) {

    }, "json");

Controller :
You can use jackson for processing json string.
Jackson is a High-performance JSON processor Java library.

    @RequestMapping(value = "/savecustomers", method = RequestMethod.POST)
    public ServiceResponse<String> saveCustomers(ModelMap model, @RequestParam String myarray) {

        try{
            ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 
            List<DtoToMAP> parsedCustomerList = objectMapper.readValue(myarray, new TypeReference<List<DtoToMAP>>() { });
            System.out.println(" parsedCustomerList :: " + parsedCustomerList);
        }catch (Exception e) {  
            System.out.println(e);
        }
    }

Note : make sure that your dto should contain same variable name as you are posting with an array object.
in my case, my dto contains firstName,lastName as am posting with an array object.

Jackson Dependancy :

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.3</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.3</version>
    </dependency>
NikhilK
  • 181
  • 2
  • 12
  • Hi! receiving JSON as string works perfectly fine. Thanks! But is there any way that mapping to java object is done automatically? – rohitpal Nov 19 '13 at 08:12
  • Okay! I have done without string now. Problem was my name of variable was not present in my form at frontend. This problem is now resolved. Thanks. – rohitpal Nov 19 '13 at 11:12
1

Check this sample for view Spring MVC configuration to use json mapper.

fjtorres
  • 266
  • 6
  • 13