Nilotpal
Nilotpal

Reputation: 3588

Two methods in Rest resource file with same @Path but different mediaType output

I have 2 methods in my Java Rest resource file with same @Path uri but different @produces. The code below :

@GET
@Path("/messages")
@Produces(MediaType.APPLICATION_XML)
 public List<Message> getAllMessages() {


    return new ArrayList<Message>(service.getMessageMap().values());
}

@GET
@Path("/messages")
@Produces(MediaType.APPLICATION_JSON)
 public List<Message> getAllMessagesJSON() {


    return new ArrayList<Message>(service.getMessageMap().values());
}

when i test it with POSTMAN rest client i always get JSON output!! Can some one explain why?? And if i want to get xml as well as json outputs, what to do?? I tried changing the content-type to application/xml..but i always get json!!

Upvotes: 1

Views: 1071

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209002

Content-Type is for the type of data being sent, either by the client as a request header or by the server as a response header. So you as a client sending the header is useless, as you are not sending any data. For the client, when it want to tell the server what type it wants, it uses the Accept: <media-type> header.

When there is no Accept header set, it usually defaults to */* leaving it up for grabs which method to pick in your case.

Upvotes: 2

Related Questions