0

我编写了一个返回单词列表的 RESTful Web 服务。Word 类被注释为根元素。

我在休息客户端上对此进行了测试,它生成了 415 Unsupported 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;
    }
4

1 回答 1

2

HiAllwyn,

There are many ways of returning of the List. Here it will not able to parse into the List object as provided in your code. Please try this.... it works.... :)

@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();

}
于 2012-08-17T13:18:59.743 回答