我有一个使用Spring启动弹簧数据和angularJs的小应用程序,我可以从我的表中读取数据并将其显示在我的页面上。 我不知道的问题我不知道'知道如何显示Hal Json格式,我知道如何显示简单的Json 格式,但不是Hal json 。我希望得到json文件作为下面的响应,感谢您的帮助。因为我想在表格中同时显示" id" 和" nom" " nom& #34;
我的休息:
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;
public class CategorieRest {
@Autowired
private CategorieRepository categorieRepository;
@RequestMapping(value="/categories",method=RequestMethod.GET)
public List<Categorie> afficher()
{
return categorieRepository.findAll();
}
}
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>
Angular 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 :(得分: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();
}
}