如何将Object从angular js传递给Spring控制器

时间:2017-11-08 15:45:22

标签: angularjs spring-mvc

Angular Js方法

$scope.Register = function(){
        userProfile = {};
        userProfile.usrName = $scope.usrName;
        userProfile.email = $scope.email;
        userProfile.pswd = $scope.pswd;
        userProfile.confirmpswd = $scope.confirmpswd;
        console.log(JSON.stringify(userProfile));
        $http.post('registerUser.htm', JSON.stringify(userProfile)).then(function mySuccess(response) {
            $scope.myWelcome = response.data;
            alert($scope.myWelcome);
        }, function myError(response) {
            $scope.myWelcome = response.statusText;
            alert($scope.myWelcome);
        });

Spring MVC Controller方法

@Controller
public class LoginController{

    @RequestMapping(headers = {"Accept=application/json"}, value = "/registerUser.htm", method = RequestMethod.POST)
    public @ResponseBody String login(@RequestBody UserProfile userProfile){
        String message = "Hi Im here";
        try{
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            UserProfile userProfile2= (UserProfile)context.getBean("userProfile") ;

            System.out.println(message);
        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();            
        }

        return message;
    }
}

这总是把我带到Js中的失败方法并提醒“不支持的媒体类型”请帮助我。

0 个答案:

没有答案