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

こんにちはオールウィン、

リストを返す方法はたくさんあります。ここでは、コードで提供されている 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();

}
于 2012-08-17T13:18:59.743 に答える