Java 415 RESTful Web服务中不支持的媒体类型

Java 415 RESTful Web服务中不支持的媒体类型,java,rest,Java,Rest,我编写了一个RESTful web服务,它返回一个单词列表。 类单词被注释为根元素 我在rest客户端上测试了它,它生成了415个不受支持的MediaType。 任何人都能帮助我们做些什么来让它工作 @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("getCategoryWordListFromJSON") public List<

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

我在rest客户端上测试了它,它生成了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;
    }
@POST
@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON)
@路径(“getCategoryWordListFromJSON”)
公共列表getLearnWordListByCategory(JSONObject JSONObject){
List wordList=new ArrayList();
试一试{
String category=(String)jsonObject.get(“category”);
LOGGER.log(Level.INFO,category);
LearnWordListDao wordListDao=新的LearnWordListDaoImpl();
addAll(wordListDao.getCategoryListFor(category));
}捕获(JSONException e){
LOGGER.log(Level.INFO,e.getMessage());
}
返回词表;
}
HiAllwyn

返回列表的方法有很多种。在这里,它将无法解析为代码中提供的列表对象。 请试试这个。。。。它起作用了……)

@POST
@产生({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@使用(MediaType.APPLICATION_JSON)
@路径(“getCategoryWordListFromJSON”)
公共响应getLearnWordListByCategory(JSONObject JSONObject){
List wordList=new ArrayList();
试一试{
String category=(String)jsonObject.get(“category”);
LOGGER.log(Level.INFO,category);
LearnWordListDao wordListDao=新的LearnWordListDaoImpl();
addAll(wordListDao.getCategoryListFor(category));
}捕获(JSONException e){
LOGGER.log(Level.INFO,e.getMessage());
}
最终GenericeEntity实体=新的GenericeEntity(字列表){};
返回Response.ok().entity(entity.build();
}

仅支持基于抛弃的
JSONObject
,而不支持json库。切换到抛弃
JSONObject
并重试。您应该将请求头内容类型设置为
application/json
如何准确调用Web服务?很可能您的客户端没有发送
内容类型:application/json
头。@sogeek我已经将内容类型设置为application/json@Allwyn请看答案。它应该对你有用。如果有效,请将答案标记为正确,以便其他人知道.return Response.ok().entity(wordList.toArray(新单词[wordList.size()]).build();也是一种类型。。。
@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();

}