415 RESTful webservice中不支持的媒体类型

问题描述:

我已经编写了一个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

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