[英]HTTP Status 415 - Unsupported Media Type error when passing data from ajax to restfull server as json
[英]415 Unsupported Media Type (JAVA + Angular + RestFULL JSON)
我的一个控制器出现问题。 当我使用(jason content type)从angular向Java控制器发送(POST)数据时,我看到在主题中可见的错误。 因此,我无法捕获服务器的响应。 控制器很好地捕获了此请求,因此本例中的书已正确添加到数据库中。 Firebug显示404错误,但是当我签入Postman时,看到415不支持的媒体类型 。 如果控制器正常工作,为什么会有这样的异常。
这是示例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;
}
这只是我的一个问题。 在另一种情况下,所有工作正常。
您遇到415错误,因为您发送的请求没有正确的content-type
标头
var bookResource = $resource( $rootScope.restUrl + 'book',{}, {
save:{
method:"POST",
headers:{'Content-Type':'application/json; charset=UTF-8'}
}
});
我希望它能解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.