【发布时间】:2015-03-18 18:24:20
【问题描述】:
当我在 firebug 的控制器中使用 JSON 将 obiect 从 angular 发送到 java 控制器时,会显示错误 404。这意味着 angular 找不到此路径,但 java 中的后端会收到此消息。问题是因为我无法响应 Angular 的消息。
这是 json:
{"title":"sdtgb","authors":[{"author_id":60,"author":"Brandon Sanderson"}],"genres":[{"genre_id":14,"genre":"Satyra"}],"description":"sdtb","path_image":"19674.png"}
java 控制器:
@SuppressWarnings("finally")
@RequestMapping(value = "/rest/book", method = RequestMethod.POST)
public MessageDTO addNewBook(@RequestBody BookDTO newBook) {
MessageDTO message = new MessageDTO();
try {
bookService.addNewBook(newBook);
message.setCheck(true);
} catch (BookTitleException e) {
message.setCheck(false);
message.setDescription("Ksiązka o tym tytule juz istnieje.");
e.printStackTrace();
} finally{
return message;
}
}
这是从萤火虫角度发送消息和错误时的路径:
"NetworkError: 404 Not Found - http://localhost:8080/engineering-project-web/rest/book"
BookDTO:
public class BookDTO implements Serializable{
private static final long serialVersionUID = -5057364006691079475L;
private AuthorEntity [] authors;
private String description;
private GenreEntity [] genres;
private String title;
private String path_image;
/* getters and setters */
}
Java 脚本:
控制器:
var book = {
title : $scope.title,
authors : $scope.author,
genres : $scope.genre,
description : $scope.description,
path_image: null
}
NewBookFct.addNewBook(book)
.then( function(resolve){
if(resolve.check){
alert("Ksiazka została dodana.");
}else{
alert(resolve.description);
}
}, function(reason){
console.log(reason);
});
服务:
service.addNewBook = function(book){
var deferred = $q.defer();
bookResource.save(book)
.$promise.then( function(data){
deferred.resolve( data );
}, function(){
deferred.reject("Error during adding new book.");
});
return deferred.promise;
}
【问题讨论】:
-
你能展示你的js代码吗?
-
我发现 Chrome + Postman extension 非常适合调试 HTTP。
-
是的,我可以。我放了JS代码。
标签: java javascript json angularjs