2017-01-13 8 views
0

私はテーブルからデータを読み込んでページに表示できるSpringブートスプリングデータとangularJsを使用する小さなアプリケーションを持っています。 問題は私は新しい私はHal Json形式を表示する方法を知っていない、私はシンプルなJson形式を表示する方法を知っているが、ではないHal json。私は、以下のような応答としてjsonファイルを取得したいと思います。 「id」「nom」の両方を表から「nom」だけでなく表示したいからです。haljsonファイルの代わりにdisplayjson

私の休息:

RestResource:

package com.Benamar.dao; 

import org.springframework.data.domain.Page; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.rest.core.annotation.RepositoryRestResource; 

import com.Benamar.entities.Categorie; 
import com.Benamar.entities.Produit; 

@RepositoryRestResource 
public interface CategorieRepository extends JpaRepository<Categorie, Long>   
    {  
} 

のindex.html:

<body ng-app="MyApp"> 
    <div id="categorie" ng-controller="CategController" > 

<button ng-click="chargerCategories()" >Charger Cat</button> 

    <h1> Categorie</h1> 
    <div > 
    <table id="table-2"> 
     <thead> 
      <tr> 
       <th>Id</th> 
       <th>Nom</th> 

      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="c in categories._embedded.categories"> 

       <td>{{c.id}}</td> 
       <td>{{c.nom}}</td> 

      </tr> 
     </tbody> 

    </table> 

角度JS:私は、データベースからカテゴリを取得するときに

var app=angular.module("MyApp",[]); 
    app.controller("CategController",function($scope, $http){ 
    $scope.categories=null; 
    $scope.chargerCategories=function(){ 
    // get the values of table categories as json format (Rest findAll) 
    $http.get("http://localhost:8989/categories") 
    .then (function (success) 
    { 
    $scope.categories=success.data; 
    }, 
    function(error){console.log="erreur !!!!"} 
    ); 
    } 

}); 

は、私はこのようにそれを得た:

{ 
"_embedded": { 
    "categories": [ 
    { 
    "nom": "ordinateurs", 
    "_links": { 
     "self": { 
      "href": "http://localhost:8989/categories/1" 
       }, 
     "categorie": { 
      "href": "http://localhost:8989/categories/1" 
         } 
       } 
    }, 
    { 
    "nom": "tablets", 
    "_links": { 
     "self": { 
     "href": "http://localhost:8989/categories/2" 
       }, 
     "categorie": { 
      "href": "http://localhost:8989/categories/2" 
        } 
       } 
     }, 
{ 
"nom": "telephones", 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories/3" 
}, 
"categorie": { 
"href": "http://localhost:8989/categories/3" 
} 
} 
}, 
{ 
"nom": "calculatrices", 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories/4" 
}, 
"categorie": { 
"href": "http://localhost:8989/categories/4" 
} 
} 
}, 
{ 
"nom": "tbi", 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories/5" 
}, 
"categorie": { 
"href": "http://localhost:8989/categories/5" 
} 
} 
}, 
{ 
"nom": "readers", 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories/6" 
}, 
"categorie": { 
"href": "http://localhost:8989/categories/6" 
} 
} 
}, 
{ 
"nom": "tv", 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories/7" 
}, 
"categorie": { 
"href": "http://localhost:8989/categories/7" 
} 
} 
} 
] 
}, 
"_links": { 
"self": { 
"href": "http://localhost:8989/categories" 
}, 
"profile": { 
"href": "http://localhost:8989/profile/categories" 
} 
}, 
"page": { 
"size": 20, 
"totalElements": 7, 
"totalPages": 1, 
"number": 0 
} 
} 

が、私はこのようなJSONファイルたい:

{ 
"content": [ 
{ 
"id": 1, 
"nom": "ordinateurs", 

}, 
{ 
"id": 2, 
"nom": "tablets", 
}, 
{ 
"id": 3, 
"nom": "telephones", 
}, 
{ 
"id": 4, 
"nom": "cellulaires", 
}, 
{ 
"id": 5, 
"nom": "tbi", 
}, 
{ 
"id": 6, 
"nom": "readers", 
}, 
{ 
"id": 7, 
"nom": "tv", 
} 
] 
} 

答えて

0

を実際に私が忘れてしまいました注釈@RestControllerをクラスRestに配置すると、次のようになります。

package com.Benamar.dao; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import com.Benamar.entities.Categorie; 

@RestController 
public class CategorieRest { 
@Autowired 
private CategorieRepository categorieRepository; 
@RequestMapping(value="/categories",method=RequestMethod.GET) 
public List<Categorie> afficher() 
{ 
    return categorieRepository.findAll(); 
} 
} 
関連する問題