angularJs,Spring MVC:如何将数据发送到Spring控制器

时间:2016-05-24 15:31:54

标签: angularjs spring spring-mvc casting spring-restcontroller

我试图将我的网络应用中的图像发送到Spring RestController;但是我没有设法施放我收到的对象。

ANGULARJS - CONTROLLER:

myModule.controller('InscriptionCtrl',function($location, $http){

    var self = this;

    self.credentials = {};

    self.register = function(credentials){

        var FormRegisterDto = credentials ? {
            login : credentials.login, 
            password : credentials.password,
            title : credentials.title,
            name : credentials.name,
            firstname : credentials.firstname,
            age : credentials.age,
            email : credentials.email,
            streetNum : credentials.streetNum,
            streetBister : credentials.streetBister,
            streetName : credentials.streetName,
            city : credentials.city,
            postalCode : credentials.postalCode,
            country : credentials.country
        } : {};

        var fd = new FormData();

        fd.append('data', angular.toJson(FormRegisterDto));
        fd.append("file", this.myImage);

        $http.post(
                '/register-user', 
                fd,
                {
                    transformRequest: angular.identity,
                    headers: {'Content-Type': undefined}
                }
        ).then(
            function(response){
                // success
            },
            function(){
                // error
            }
        );
    };
});

我的Spring RestController正确接收数据和文件,因为如果我打印了#34;数据",它正在写:{"登录":" aaaaaaaaaa", "密码":" AAAAAAAAAAAA""标题":" M""电子邮件":" AAAAAAAAAA @ AAAAAAAAAA"}

我的问题是我想要投射对象"数据"到我的班级" FormRegisterDto"其中包含" String login"," String password"等属性。等...

我的JAVA代码:

@RequestMapping(value = "/register-user-blob", produces = "application/json")
public FormRegisterResponseDto registerUserWithBlob(
        @RequestParam(value="file", required=false) MultipartFile file,
        @RequestParam(value="data") Object data){


    FormRegisterRequestDto request = (FormRegisterRequestDto) data; // it doesn't works

    System.out.println(data); // it show {"login":"aaaaaaaaaa","password":"aaaaaaaaaaaA","title":"M","email":"aaaaaaaaaa@aaaaaaaaaa"}

    return new FormRegisterResponseDto();
}

你知道怎么做吗?谢谢;)。

1 个答案:

答案 0 :(得分:1)

您的<input type="text" data-field-name="label" class="form-control family_member_label save_on_change" placeholder="Enter a name" value="<%= this.model.get('label') %>">\ 类型为Object data,因此无法将其强制转换为String,您要做的是将前端的对象序列化,将其转换为json字符串(其中)它似乎是在这一点上)而不是像这样使用Gson google的库

FormRegisterRequestDto