AngularJs形成post数据,在我的spring控制器中给出null值

时间:2016-10-03 22:03:15

标签: javascript angularjs spring spring-mvc null

大家好我想尝试使用angular发布一个表单,但我在我的spring控制器中得到空值。在我的控制台中,我看到了sysout的空值.Moreover即使看到公牛打印也会收到错误警告在我的控制台上。

我的JS控制器

angular.module('ngMailChimp', ['ngAria', 'ngMessages', 'ngAnimate'])
.controller('SignUpController', function ($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
    var ctrl = this,
        newCustomer = { firstName:'',lastName:'',email:'',streetName:'',aptName:'',cityName:'',stateName:'',countryName:'', zipName:'', userName:'',password:'' };

    var signup = function () {
        if( ctrl.signupForm.$valid) {
            ctrl.showSubmittedPrompt = true;
            var formData = {
                    'firstName' : $scope.ctrl.newCustomer.firstName,
                    'lastName' : $scope.ctrl.newCustomer.lastName,
                    'email' : $scope.ctrl.newCustomer.email,
                    'streetName' : $scope.ctrl.newCustomer.streetName,
                    'aptName' : $scope.ctrl.newCustomer.aptName,
                    'cityName' : $scope.ctrl.newCustomer.cityName,
                    'stateName' : $scope.ctrl.newCustomer.stateName,
                    'countryName' : $scope.ctrl.newCustomer.countryName,
                    'zipName' : $scope.ctrl.newCustomer.zipName,
                    'userName' : $scope.ctrl.newCustomer.userName,
                    'password' : $scope.ctrl.newCustomer.password
            };

            var response = $http.post('http://localhost:8080/Weber/user/save', JSON.stringify(formData));
            response.success(function(data, status, headers, config) {
                $scope.list.push(data);
            });
            response.error(function(data, status, headers, config) {
                alert( "Exception details: " + JSON.stringify({data: data}));
            });

        }
    };

我的Spring控制器

@RestController
@RequestMapping(value = "/user")
public class UserRegistrationControllerImpl implements UserRegistrationController  {

@Autowired
UserRegistrationDao userDao;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveUser(UserRegistration userReg) {
    System.out.println(userReg.getFirstName()+" "+userReg.getLastName());
    userDao.registerUser(userReg);
    return "success";
}

请帮帮我

谢谢 标记

2 个答案:

答案 0 :(得分:1)

没有指定用于将JSON转换为Java对象的映射器。

如果您希望将JSON转换为UserRegistration的对象,请使用Jackson(dore,databind,annotations)。

检查出来:Convert nested java objects to Jackson JSON

需要在dispatcher-servlet中添加以下内容。这是为了将JSON映射到Java对象:

<beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>

编辑1: 控制器中的方法是这样的吗?

@RequestMapping(value = "/save", method = RequestMethod.POST, headers = "Accept=application/json")
public String saveUser(@RequestBody UserRegistration userReg) {
    System.out.println(userReg.getFirstName()+" "+userReg.getLastName());
    userDao.registerUser(userReg);
    return "success";
}

如果您没有回复网页并且结果被消费,请使用上述内容。如果您希望从此方法返回某些内容并显示在网页中或在其他地方使用,则方法声明将更改为:

public @ResponseBody String saveUser(@RequestBody UserRegistration userReg)

编辑2:

    $scope.post = function() {
        $scope.data = null;
        $http({
            method : 'POST',
            url : 'save',
            params : {
            firstName : $scope.ctrl.newCustomer.firstName,
            lastName : $scope.ctrl.newCustomer.lastName,
            email : $scope.ctrl.newCustomer.email,
            streetName : $scope.ctrl.newCustomer.streetName,
            aptName : $scope.ctrl.newCustomer.aptName,
            cityName : $scope.ctrl.newCustomer.cityName,
            stateName : $scope.ctrl.newCustomer.stateName,
            countryName : $scope.ctrl.newCustomer.countryName,
            zipName : $scope.ctrl.newCustomer.zipName,
            userName : $scope.ctrl.newCustomer.userName,
            password : $scope.ctrl.newCustomer.password
            }
        }).success(function(data, status, headers, config) {
             $scope.list.push(data); 
        }).error(function(data, status, headers, config) {
            alert("Exception");
        });
    };

答案 1 :(得分:0)

尝试在方法参数中添加@RequestBody

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String saveUser(@RequestBody UserRegistration userReg) {
    System.out.println(userReg.getFirstName()+" "+userReg.getLastName());
    userDao.registerUser(userReg);
    return "success";
}