2016-05-10 11 views
0

不正な要求を返すSpringBootを呼び出します。

package com.agTest.entities; 

import java.io.Serializable; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity 
public class Product implements Serializable{ 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Long idProduct; 
    private String description; 
    private double prix; 


    public Product() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 


    public Product(String description, double prix) { 
     super(); 
     this.description = description; 
     this.prix = prix; 
    } 


    public Long getIdProduct() { 
     return idProduct; 
    } 
    public void setIdProduct(Long idProduct) { 
     this.idProduct = idProduct; 
    } 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 
    public double getPrix() { 
     return prix; 
    } 
    public void setPrix(double prix) { 
     this.prix = prix; 
    } 

} 

ここに私のProductRepositoryインタフェースがあります使用してJpaRepository:

package com.agTest.services; 

import java.util.List; 

import com.agTest.entities.Product; 

public interface ProductMetier { 
    public Product saveProduct(Product p); 
    public List<Product> getProducts(); 

} 
:ここ

package com.agTest.Dao; 

import org.springframework.data.jpa.repository.JpaRepository; 

import com.agTest.entities.Product; 

public interface ProductRepository extends JpaRepository<Product, Long>{ 

} 

は私のProductMetierインタフェースでありますここ

は私のProductMetierImplクラスである:ここ

package com.agTest.services; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import com.agTest.Dao.ProductRepository; 
import com.agTest.entities.Product; 
@Service 
public class ProductMetierImpl implements ProductMetier{ 

    @Autowired 
    private ProductRepository productRepository; 
    @Override 
    public Product saveProduct(Product p) { 
     productRepository.save(p); 
     return p; 
    } 

    @Override 
    public List<Product> getProducts() { 

     return productRepository.findAll(); 
    } 

} 

は私@RestControllerクラスである:ここ

package com.agTest.RestServices; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
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.agTest.entities.Product; 
import com.agTest.services.ProductMetier; 

@RestController 
public class ProductRestService { 

    @Autowired 
    private ProductMetier productMetier; 

    @RequestMapping(value="/products", method=RequestMethod.POST) 
    public Product saveProduct(@RequestBody Product p){ 
     return productMetier.saveProduct(p); 
    } 

    @RequestMapping(value="/products", method=RequestMethod.GET) 
    public List<Product> getProducts(){ 
     return productMetier.getProducts(); 
    } 

} 

は私のapplication.propertiesです:

# Datasource settings: 
spring.datasource.url = jdbc:mysql://localhost:3306/agence_test 
spring.datasource.username = root 
spring.datasource.password = 
spring.datasource.driver-class-name = com.mysql.jdbc.Driver 

spring.jpa.database = MYSQL 

spring.jpa.show-sql = true 

spring.jpa.hibernate.ddl-auto = update 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

私は郵便配達で、私のAPIをテストしていますすべてうまくいっていますが、angularjsクライアントを作成しようとすると、http Postメソッドは例外を返します。

ここ

は私angularjsコントローラです。ここ

var app=angular.module("AVG",[]); 
app.controller("AVGController",function($scope,$http){ 

    $scope.products=[]; 
    $scope.description=null; 
    $scope.prix=null; 

    $scope.getProducts= function(){ 

     $http.get("/products") 
     .success(function(data){ 
      $scope.products=data; 
     }) 
     .error(function(data){ 
      alert("error"); 
     }); 

    }; 
    // headers :{'Content-Type':'application/json'} 
    $scope.saveMembre= function(){ 
     $http({ 
       method : 'POST', 
       url : "/products", 
       data : "description="+$scope.description+"prix="+$scope.prix, 
       headers :{'Content-Type':'application/json'} 


     }) 
     .success(function(data){ 
      alert("success"); 
     }) 
     .error(function(data){ 
       alert("error"); 
      }); 


    }; 



}); 

は私のindex.htmlです:

Hibernate: select product0_.id_product as id_produ1_0_, product0_.description as descript2_0_, product0_.prix as prix3_0_ from product product0_ 
Hibernate: select product0_.id_product as id_produ1_0_, product0_.description as descript2_0_, product0_.prix as prix3_0_ from product product0_ 
Hibernate: select product0_.id_product as id_produ1_0_, product0_.description as descript2_0_, product0_.prix as prix3_0_ from product product0_ 
Hibernate: select product0_.id_product as id_produ1_0_, product0_.description as descript2_0_, product0_.prix as prix3_0_ from product product0_ 
Hibernate: select product0_.id_product as id_produ1_0_, product0_.description as descript2_0_, product0_.prix as prix3_0_ from product product0_ 
2016-05-10 18:03:33.978 WARN 5420 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'description': was expecting ('true', 'false' or 'null') 
at [Source: [email protected]; line: 1, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'description': was expecting ('true', 'false' or 'null') 
at [Source: [email protected]; line: 1, column: 13] 

ます。http:ここ

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<link rel="stylesheet" type="text/css" href=""bootstrap-3.3.6-dist/css/bootstrap.min.css""/> 
<title>Agence Test</title> 
</head> 
<body ng-app="AVG" ng-controller="AVGController"> 
    <h1>AVE.com</h1> 
    <!-- Sauvegarde des Membres --> 
    <div> 

    <form> 
    <label>Description</label> 
    <input type="text" ng-model="description"/> 
    <label>Prix</label> 
    <input type="text" ng-model="prix"/> 
    <button ng-click="saveMembre()"> SAVE </button> 
    </form> 

    </div> 



    <!-- Affichage des Membres --> 
    <div > 
    <button type="button" ng-click="getProducts()">Get Products</button> 
    <table> 
    <tr> 
    <th>ID</th> <th>DESCRIPTION</th> <th>PRIX</th> 
    </tr> 
    <tr ng-repeat="item in products"> 
    <td>{{item.idProduct}}</td> 
    <td>{{item.description}}</td> 
    <td>{{item.prix}}</td> 
    </tr> 
    </table> 
    <!-- <ul> 
     <li ng-repeat="item in membres"> {{item.idMembre}}</li> 
     <li ng-repeat="item in membres"> {{item.nomMembre}}</li> 
    </ul> --> 
</div> 

    <script type="text/javascript" src="angular/angular.min.js"></script> 
    <script type="text/javascript" src="js/app.js"></script> 

</body> 
</html> 

は、HTTP POSTメソッドを呼び出すときに例外が返されますgetメソッドは期待通りに機能する

p私のPEFに必要な助けを借りて、私はそれに多くの時間を費やしました!!

答えて

0

$ http postによって予期される 'data'はオブジェクトであり、文字列ではありません。 試してみてください。

data : {"description":$scope.description, "prix":$scope.prix} 
+0

は本当に私はそれが動作BROあなた –

0

あなたの春のコードに問題はありません。ここでの唯一の問題は、あなたの要求はあなたが行っている値を確認することができますが、この形式で

data : {"property1":value1, "property2": value2, ....,"propertyN" : value"} 

をリクエストを送信する必要があり、あなたのAjaxリクエスト($ HTTP)での値

を送信している方法です開発者ツールのネットワークタブ。

これは何らかの助けとなることを望みます。

ハッピー学習:)

+0

感謝を考える方法がわからない、兄弟ティエリーを考えています –

関連する問題