Srinath Murugula
Srinath Murugula

Reputation: 582

Getting 400 Bad Request while sending angularjs post request with objects

Ui code is

    var page1={"name":"raj","acc":"123"};
    var page2={"name1":"sathi","acc2":"321"};

    var finl={"page1":page1,"page2":page2};

    var response=$http.post("myfirsturl",{"page1":page1,"page2":page2});

spring controller code is

    @RequestMapping(value="/myfirsturl",method=RequestMethod.POST)
public String hello1(@RequestBody sampledetails details){

    page1 obj=details.getPag1();
    page2 obj2=details.getPag2();

    System.out.println(obj.getName()+"  "+obj.getAcc());
    System.out.println(obj2.getName1()+"  "+obj2.getAcc2());


    return "";
}

sampledetails class

  public class sampledetails implements Serializable{


  private page1 pag1;
   private page2 pag2;

   //setters and getters

page1 class

 public class page1 implements Serializable{

  private String name;
  private String acc;
  //setters and getters

page2 class public class page2 implements Serializable{

  private String name1;
  private String acc2;
  //setters and getters

I have tried sending object it is working for primitive types but not working for objects.

Upvotes: 0

Views: 885

Answers (1)

Pratap A.K
Pratap A.K

Reputation: 4517

It's pag1 not page1 acccording to your sampledetails bean class. You must use member variable name.

Change this

var response=$http.post("myfirsturl",{"page1":page1,"page2":page2});

To this

var response=$http.post("myfirsturl",{"pag1":page1,"page2":pag2});

Or like this

$http.post('/myfirsturl', {         
            "pag1" : page1, "pag2" : page2
        }).success(function(data){
            alert("Success");
        })

Controller

@RestController
public class Controller {

     @RequestMapping(value="/myfirsturl",method=RequestMethod.POST)
     public String hello1(@RequestBody Sampledetails details){

         System.out.println("Inside");

         Page1 obj=details.getPag1();
         Page2 obj2=details.getPag2();

         System.out.println(obj.getName()+"  "+obj.getAcc());
         System.out.println(obj2.getName1()+"  "+obj2.getAcc2());


         return "";
     }

}

Upvotes: 1

Related Questions