2012-08-17 81 views
0

我已經編寫了一個RESTful Web服務,它返回一個單詞列表。 類Word被註釋爲根元素。415 RESTful webservice中不支持的媒體類型

我在靜態客戶端上測試了它生成的415不支持的MediaType。 任何人都可以幫助還有什麼必須做,使其工作。

@POST 
    @Produces(MediaType.APPLICATION_JSON) 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Path("getCategoryWordListFromJSON") 
    public List<Word> getLearnWordListByCategory(JSONObject jsonObject) { 
     List<Word> wordList = new ArrayList<Word>(); 
     try { 
      String category = (String) jsonObject.get("category"); 
      LOGGER.log(Level.INFO, category); 
      LearnWordListDao wordListDao = new LearnWordListDaoImpl(); 
      wordList.addAll(wordListDao.getCategoryListFor(category)); 
     } catch (JSONException e) { 
      LOGGER.log(Level.INFO, e.getMessage()); 
     } 
     return wordList; 
    } 
+0

只支持Jettison的JSONObject',而不是json庫的。切換到拋棄'JSONObject'並嘗試。 – 2012-08-17 10:22:51

+0

您應該將請求標頭內容類型設爲'application/json' – 2012-08-17 11:21:44

+0

您究竟如何調用webservice?很有可能你的客戶端不會發送'Content-Type:application/json'頭文件。 – 2012-08-17 11:36:32

回答

2

HiAllwyn,

有清單的返回的許多方面。在這裏它不能解析到代碼中提供的List對象。 請嘗試....它的作品.... :)

@POST 
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) 
@Consumes(MediaType.APPLICATION_JSON) 
@Path("getCategoryWordListFromJSON") 
public Response getLearnWordListByCategory(JSONObject jsonObject) { 
    List<Word> wordList = new ArrayList<Word>(); 
    try { 
     String category = (String) jsonObject.get("category"); 
     LOGGER.log(Level.INFO, category); 
     LearnWordListDao wordListDao = new LearnWordListDaoImpl(); 
     wordList.addAll(wordListDao.getCategoryListFor(category)); 
    } catch (JSONException e) { 
     LOGGER.log(Level.INFO, e.getMessage()); 
    } 


final GenericEntity<List<Word>> entity = new GenericEntity<List<Word>>(wordList) { }; 
     return Response.ok().entity(entity).build(); 

} 
+0

return Response.ok()。entity(wordList.toArray(new Word [wordList.size()]))。build();也是一種類型... – NamingException 2012-08-17 13:20:53

相關問題