Allwyn
Allwyn

Reputation: 53

415 Unsupported Media Type in RESTful webservice

I have written a RESTful web service that returns a list o f words. The class Word is annotated as the root element.

I tested this on rest client it generated 415 Unsupported MediaType. Can anyone help what else has to be done to make it work.

@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;
    }

Upvotes: 0

Views: 6276

Answers (1)

NamingException
NamingException

Reputation: 2404

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

}

Upvotes: 2

Related Questions