2017-07-11 30 views
0

enter image description here角度js-1(loginCtrl):角度不反映彈簧響應,我不明白可能是什麼問題......可以請你協助,順便是新的角度和春季的RESTful-WSJava Spring 4.0響應狀態碼200,但角度Js執行(錯誤登錄)響應的錯誤塊

app.controller('loginCtrl', function($scope,$http,$window){ 

      $scope.login = function(email,password) { 
      var myData = { 
      email: $scope.email, 
      password: $scope.password 
      }; 

      $http.post(customerURL,JSON.stringify(myData)).then(function(response){ 
       if(response.data) 
       console.log(response.data); 
       $scope.msg = "SUCCESSFULLY LOGGED IN"; 
       $scope.status = response.status; 
       $scope.statusText = response.statusText; 
       console.log($scope.msg);  
       },function(response){ 
       //console.log(response.status); 

       $scope.msg = "ERROR LOGIN IN"; 

       console.log($scope.msg); 


      }); 
     };                          
     }); 

Java的春季4.0:找到電子郵件和散列密碼正確(使用checkpw)...

@RequestMapping(value="",method = RequestMethod.POST) 
    public ResponseEntity LoginCustomer(@RequestBody String json) throws CustomerNotFoundException 
    { 
     try { 
      ObjectMapper mapper = new ObjectMapper(); 
      final JsonNode data = mapper.readTree(json); 

      String email = data.get("email").asText(); 
      String password = data.get("password").asText(); 


      if(services.findByName(email) != null && services.findByPw(password) != null){ 
      logger.info("received correct user details in :json String " + json); 
      System.out.println("Successfull!!"); 
      return new ResponseEntity(HttpStatus.OK); 

      }else{ 
       System.out.println("Error!!"); 
       return new ResponseEntity(HttpStatus.BAD_REQUEST); 
      } 
     } catch (IOException ex){ 
     throw new RuntimeException(ex); 
     } 

    } 

我現在的客戶控制器...

控制器類之前
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.product.Controller; 

import com.product.Exceptions.CustomerNotFoundException; 
import com.product.Services.CustomerServices; 
import com.product.Product.Customer; 

import com.fasterxml.jackson.databind.JsonNode; 
import com.fasterxml.jackson.databind.ObjectMapper; 


import java.io.IOException; 
import java.io.Serializable; 
import java.util.List; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.security.crypto.bcrypt.BCrypt; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
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 org.springframework.web.servlet.ModelAndView; 

/* 
* 
* @author HP 
*/ 
@RestController 
@RequestMapping("/customer") 
public class CustomerController{ 
    /////////members //////////////////////////////////////////////////////////////////////////////////// 
    private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);   // 
    @Autowired                      // 
    private CustomerServices services; 
    private BCryptPasswordEncoder pe;// 
    /////////////////////////////////////////////////////////////////////////////////////////////////// 

    @RequestMapping(value="",method = RequestMethod.PUT) 
    public ResponseEntity createCustomer(@RequestBody String json) throws CustomerNotFoundException 
    { 
     try { 
      ObjectMapper mapper = new ObjectMapper(); 
      final JsonNode data = mapper.readTree(json); 
      logger.info("recived json String " + json); 
      String email = data.get("email").asText(); 
      String password = data.get("password").asText(); 
      String firstname = data.get("firstname").asText(); 
      String surname = data.get("surname").asText(); 

      //brypt hashing 
      String pw_hash = BCrypt.hashpw(password,BCrypt.gensalt(10)); 

       Customer c = new Customer(firstname, surname, email, pw_hash); 


      ///////create customer ///////// 
      services.createCustomer(c); 
      ///return if try was succesful 
      //loginService.createLogin(u); 
      return new ResponseEntity(HttpStatus.CREATED); 

     } catch (IOException ex){ 
     throw new RuntimeException(ex); 
     } 

    } 

    @RequestMapping(value="",method = RequestMethod.POST) 
    public ResponseEntity LoginCustomer(@RequestBody String json) throws CustomerNotFoundException 
    { 
     try { 
      ObjectMapper mapper = new ObjectMapper(); 
      final JsonNode data = mapper.readTree(json); 

      String email = data.get("email").asText(); 
      String password = data.get("password").asText(); 


      if(services.findByName(email) != null && services.findByPw(password) != null){ 
      logger.info("received correct user details in :json String " + json); 
      System.out.println("Successfull!!"); 
      return new ResponseEntity(HttpStatus.OK); 

      }else{ 
       System.out.println("Error!!"); 
       return new ResponseEntity(HttpStatus.BAD_REQUEST); 
      } 
     } catch (IOException ex){ 
     throw new RuntimeException(ex); 
     } 

    } 



     @RequestMapping(value = "", method = RequestMethod.GET) 
    public ResponseEntity<List<Customer>> getAllCustomers() { 

     List<Customer> customerList = services.getCustomerList(); 
     if(customerList != null) { 
      return new ResponseEntity<>(customerList, HttpStatus.OK); 
     }else{ 
      return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 
     } 
    } 

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
    public ResponseEntity<CustomerController> Delete(@PathVariable long id) throws CustomerNotFoundException { 
    services.DeleteCustomer(id); 
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 
    } 
} 
+0

你得到了什麼迴應? –

回答

0

使用波紋管註釋:

@RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") 
    public ResponseEntity save(@RequestBody LoginView loginView) { 


} 

LoginView之前

@RestController 
@RequestMapping("/login") 

使用是含有可使用成JSON及其設定器getter方法相同的變量的類。

package com.test; 



public class LoginView { 


private String email; 

private String password; 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

} 
0

感謝您的時間,你們誰通過我的崗位去,後是清晰的聲音,因爲我能解決這個問題,最終的問題是,狀態碼總是訴諸到200甚至當url不存在,我阻止了默認和傳播在客戶端,停止請求被取消...希望這可以幫助某人,順便說這是我的第一個Restful應用程序...我是最快樂的地球人......;)