在使用对象发送angularjs post请求时获取400 Bad Request

时间:2016-01-22 11:25:40

标签: angularjs spring-mvc

Ui代码是

    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});

弹簧控制器代码是

    @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

第2页课程       public class page2实现了Serializable {

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

我尝试过发送对象,它正在处理原始类型但不适用于对象。

1 个答案:

答案 0 :(得分:1)

根据您的sampledetails bean类,它是pag1而不是page1。您必须使用成员变量名。

更改此

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

到此

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

或者像这样

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

控制器

@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 "";
     }

}