Witam,
W ramach nauki testuje aktualnie komunikacje spring mvc z angularjs, pojawił się pewien problem.
Problem polega na zwracanym formacie odpowiedzi z listą obiektów Article. W przypadku pobrania całej listy (/article) poszczególne obiekty w odpowiedzi (angujarjs) wyglądają następująco:
content: "zawartosc"
date: "1980-11-15"
id: 1
tittle: "fujlzknaav"
natomiast w przypadku pobrania listy według jakiegoś warunku (w poniższym przypadku po id), odpowiedz wygląda jak poniżej.
0: 6
1: "zawartosc"
2: "2006-01-24"
3: "gpuiwgzytv"
4: 3
length: 5
Czy jest ktoś w stanie wytłumaczyć jaka jest tego przyczyna? Czy o czymś zapomniałem? I pytanie jaki jest najlepszy sposób na ujednolicenie?
Jeżeli kod jest nie wystarczający dorzucę dalszą część.
model danych (+ gettery/settery)
@Entity
@Table(name = "ARTYKULY")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "TYTUL")
private String tittle;
@Temporal(TemporalType.DATE)
@Column(name = "DATA_DODANIA", unique = false, nullable = true, length = 10)
private Date date;
@Column(name = "ZAWARTOSC")
private String content;
public Article() {
}
public Article(String tittle, Date date, String content) {
this.tittle = tittle;
this.date = date;
this.content = content;
}
Kontrollery
@RequestMapping(value = "/article/{categoryId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Article>> getArticleByCategory(@PathVariable("categoryId") int categoryId) {
List<Article> articles = articleService.getAllByCategoryId(new Integer(categoryId));
if (articles == null || articles.isEmpty()) {
return new ResponseEntity<List<Article>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Article>>(articles, HttpStatus.OK);
}
@RequestMapping(value = "/article", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Article>> getALlArticles() {
List<Article> articles = articleService.getAll();
if (articles == null || articles.isEmpty()) {
return new ResponseEntity<List<Article>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Article>>(articles, HttpStatus.OK);
}
service_article
'use strict';
App.factory('ArticleService', ['$http', '$q', function($http, $q){
return {
getArticles: function(id) {
return $http.get('/spring-angular-test/article2/' + id)
.then(
function(response) {
return response.data;
},
function(error) {
return $q.reject(error);
}
);
},
getAllArticles: function() {
return $http.get('/spring-angular-test/article')
.then(
function (response) {
return response.data;
},
function (error) {
return $q.reject(error);
}
);
}
};
}]);
controller_article
'use strict'
App.controller('ArticleController', ['$scope', 'ArticleService', function($scope, ArticleService){
var self = this;
self.article = {id:null, tittle:'', date:null, content:''};
self.articles = [];
self.getArticles = function(id) {
ArticleService.getArticles(id)
.then(
function(d) {
self.articles = d;
},
function(error) {
console.error("blad")
}
);
};
self.getAllArticles = function() {
ArticleService.getAllArticles()
.then(
function(d) {
self.articles = d;
},
function(error) {
console.error("blad")
}
);
}
self.getAllArticles();
}]);