Java Spring 4嵌套对象InvalidPropertyException

时间:2016-12-16 12:02:22

标签: java angularjs spring

我遇到嵌套对象的问题:每次崩溃时都会出现异常

  1. 在我的列表中使用数字格式例外
  2. 在我的对象上 org.springframework.beans.InvalidPropertyException
  3. 你能帮帮我错误吗?为什么没有正确解释帖子正文?

    这是AngularJS

    var form = { text1: $scope.text1,
                     text2: $scope.text2};
    
    var d ={    one: "textone",
                two: "texttwo",
           myObject: form,
       myListString: ["one", "two"] };
    
    var toPost =   $.param(d);
    $http({
             method:'post',
                url:'http://localhost:8080/TestSpringPrj/test',
               data:toPost,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           dataType:"json"
                   })
    

    我的Java类:

    public class MyClassTest{
        private String one;
        private String two;
        private MyObjForm myObject;
        private List<String>  myListStrin
    
        public MyClassTest() {
            super();
            // TODO Auto-generated constructor stub
        }
        ///////GETTERS & SETTERS
    }
    

    我的控制器

    @Controller
    public class RicercaAvanzataT {
        @RequestMapping(value="/test")
        public ModelAndView processSubmit(@ModelAttribute MyClassTesttest) {
            ModelAndView mv = new ModelAndView("response");
            System.out.println("Test was fine");
            return mv;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

你需要使用@RequestBody,它将你的json请求的整个主体传递给转换器,然后传递给控制器​​方法。而@ModelAttribute只是从HttpServletRequest填充模型

@RequestMapping(value="/test")
    public ModelAndView processSubmit(@RequestBody MyClassTesttest) {
        ModelAndView mv = new ModelAndView("response");
        System.out.println("Test was fine");
        return mv;
    }