我的一个控制器存在问题。当我使用(jason内容类型)从angular发送(POST)数据到java控制器时,我看到主题中可见的错误。所以我无法从服务器上获取响应。控制器捕获此请求很好,因此在这种情况下,书籍正确地添加到数据库。 Firebug显示404错误,但当我在Postman中查看时,我看到 415 Unsupported Media Type 。如果控制器正常工作,为什么会出现这种情况。
这是示例JSON:
{"title":"fgthbfgd","authors":[{"author_id":24,"author":"danielle steel"}],"genres":[{"genre_id":1,"genre":"Dramat"}],"description":"rthg","path_image":"19296.png"}
这是控制器:
@SuppressWarnings("finally")
@RequestMapping(value = "/rest/book", consumes = "application/json", produces = "application/json", 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;
}
}
这是BookDTO
public class BookDTO implements Serializable{
private static final long serialVersionUID = -5057364006691079475L;
private Integer id;
private AuthorEntity [] authors;
private String description;
private GenreEntity [] genres;
private String title;
private String path_image;
private double rate;
private Integer progressBar;
private boolean flagRate;
private double userRate;
/* geters and seters */
}
这是Angular代码:
var bookResource = $resource( $rootScope.restUrl + 'book');
var book = {
title : $scope.title,
authors : $scope.author,
genres : $scope.genre,
description : $scope.description,
path_image: null
}
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;
}
这只是我的一个问题。在另一个案例中,所有工作正常。
答案 0 :(得分:1)
您有一个415错误,因为您发送的请求没有正确的content-type
标题
var bookResource = $resource( $rootScope.restUrl + 'book',{}, {
save:{
method:"POST",
headers:{'Content-Type':'application/json; charset=UTF-8'}
}
});
我希望它能解决你的问题