2015-04-02 76 views
0

我的一個控制器出現了問題。當我發送(POST)數據從角度到Java控制器使用(傑森內容類型)時,我看到在主題中可見的錯誤。所以我無法捕捉到服務器的響應。控制器抓住這個請求是好的,所以在這個案例書中正確地添加到數據庫。 Firebug顯示404錯誤,但當我檢查郵遞員時,我看到415不支持的媒體類型。如果控制器工作正常,爲什麼會有這樣的例外。415不受支持的媒體類型(JAVA + Angular + RestFULL JSON)

這是示例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 */ 
} 

這是角代碼:

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

可以u顯示當然BookDTO類結構 – Anita 2015-04-02 13:28:03

+0

。我編輯了我的帖子。 – user2590727 2015-04-02 13:38:51

+0

您可以添加angularjs電話嗎? – jmgross 2015-04-02 13:44:08

回答

1

你有一個415錯誤,因爲你沒有正確的content-type

var bookResource = $resource($rootScope.restUrl + 'book',{}, { 
    save:{ 
     method:"POST", 
     headers:{'Content-Type':'application/json; charset=UTF-8'} 
    } 
}); 

我希望這將解決您的問題發送請求

+0

不幸的是沒有解決問題。 :( – user2590727 2015-04-02 14:38:53

+0

我做了一個錯誤替換'後:'與'保存:'我更新了我的帖子 – jmgross 2015-04-02 14:40:42

+0

它也沒有幫助.... :( – user2590727 2015-04-02 14:56:51

相關問題