从角度客户端传递数据时,在spring控制器中获取null值

时间:2018-06-11 17:24:21

标签: java angularjs spring rest

我有一个角度客户端,我从中调用spring RestController。当我从postman传递数据时,我在spring控制器中获取值,但是当我将数据从客户端传递给控制器​​时。

/**

*服务  * / app.factory(' Userservice',函数($ HTTP){

var userservice={}

userservice.Signup=function(user){
    console.log("In User service Signup",user)
    return $http.post("http://localhost:8080/Niit-Social/Signup", user)
}

userservice.Login=function(user){
    console.log("In User service Login",user)
    return $http.post("http://localhost:8080/Niit-Social/Login", user)
}

return userservice;

})

 /**
 * Controller
 */
  app.controller('Userctrl',function($scope,Userservice,
 $location,$rootScope,$cookieStore)
{
$scope.Signup=function(user)
{
console.log("In User controller Signup",user)
Userservice.Signup(user).then(function (response){

console.log(response.data)
console.log(response.status)
$location.path('/')
},
function(response)
{
$scope.error=response.data 
console.log(response.data)
console.log(response.status)    
})
}

$scope.Login=function(user)
{
console.log("In User Controller Login",user)
Userservice.Login(user).then(function(response){
console.log("Creating rootscope variable and assigning data")
$rootScope.LoggedInUser=response.data
$cookieStore.put('LoggedInUser',response.data)
console.log("Data set in cookie")
console.log(response.data)
console.log(response.status)
$location.path('/')
},
function(response)
{
console.log(response.data)
console.log(response.status)
})  
}
})

这是我的控制器

package com.niit.controllers;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.niit.Dao.UserDao;
import com.niit.model.Errorclass;
import com.niit.model.User;

@RestController
public class UserController {
@Autowired
private UserDao userdao;

 public UserController()
 {
    System.out.println("UserController class  Instantiated");
 }

  @RequestMapping(value="/Signup",method=RequestMethod.POST)
  public ResponseEntity<?> Registration(@RequestBody User user)
 {

    System.out.println("In UserController Registration function Invoked");
    if(userdao.isEmailUnique(user.getEmail()))
    {
    System.out.println("User with username "+user.getEmail()+" Not Present - 
    --> Registering user"); 
    userdao.Registration(user);
    System.out.println("User registered successfully");
    return new ResponseEntity<User>(user,HttpStatus.CREATED);
    }
    else
    {
    System.err.println("A User with username " + user.getEmail() + " already 
    exists ---> User cannot be registered");
    Errorclass ec=new Errorclass(10,"Username already exists.Please choose a 
    different username");
    return new ResponseEntity<Errorclass>(ec,HttpStatus.CONFLICT);
    }
  }

    @RequestMapping(value="/Login",method=RequestMethod.POST)
    public ResponseEntity<?> Login(@RequestBody User user,HttpSession 
    session)
   {
    System.out.println("In UserController Login function Invoked");
    System.out.println("Validating  User Credentials");
    System.out.println("User-Email--->"+user.getEmail()+"   "+"User- 
    password--->"+user.getPassword());
    User validuser=userdao.Login(user);
    if(validuser==null)
    {   
        System.err.println("User Credentials are  Incorrect");
        Errorclass ec=new Errorclass(27,"Invalid Credentials");
        return new ResponseEntity<Errorclass>(ec,HttpStatus.NOT_FOUND);
    }
    else
    {
    System.out.println("User Credentials are correct ");
    System.out.println("Setting attribute in session Object.Session id--- 
    >"+session.getId());
    session.setAttribute("email", user.getEmail());
    validuser.setOnline(true);
    System.err.print("Changing Online status of user to TRUE");
    userdao.UpdateUserOnlineStatus(validuser);
    System.out.println("User Online-status Updated");
    return new ResponseEntity<User>(validuser,HttpStatus.OK);
    }
  }

   @RequestMapping(value="/GetAllUsers",method=RequestMethod.GET)
   public ResponseEntity<?>GetAllUsers()
   {
    System.out.println("In UserController GetAllUsers function Invoked");
    System.out.println("Getting all users list");
    List<User>lou=userdao.GetAllUsers();    

    if(lou.isEmpty())
    {
    Errorclass ec=new Errorclass(28,"List is empty");
    return new ResponseEntity<Errorclass>(ec,HttpStatus.NO_CONTENT) ;
    }
    return new ResponseEntity<List<User>>(lou,HttpStatus.OK);
   }
}

output on webbrower console
In User Controller Login {Email: "Manik17@gmail.com", Password: "Mk17"}
In User service Login {Email: "Manik17@gmail.com", Password: "Mk17"}
angular.js:10967 POST http://localhost:8080/Niit-Social/Login 404 (Not 
Found)

 Eclipse console output
In UserController Login function Invoked
Validating  User Credentials
User-Email--->null   User-password--->null

1 个答案:

答案 0 :(得分:0)

您可以更改请求映射,如下所示

@RequestMapping(value =&#34; / test&#34;,method = RequestMethod.POST,consume =&#34; application / json&#34;,produce =&#34; application / json&#34;)