PUT - Sending JSON - Unsupported Media Type
I have a REST service in Jersey , which I am implementing using Postman
I get the following error in postman :
415 Unsupported Media Type
I tried this , this , this and this . Nothing Works.
I am sending JSON and the request method is PUT
I am trying to update a resource through a method in a service , called
updateMessage.
My Code :
package org.glassfish.jersey.archetypes.jersey_quickstart_webapp.resources;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.archetypes.jersey_quickstart_webapp.model.Message;
import org.glassfish.jersey.archetypes.jersey_quickstart_webapp.service.MessageService;
@Path("/messages")
public class MessageResource {
MessageService messageService = new MessageService();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Message> getMessages()
{
return messageService.getAllMessages();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Message addMessage(Message message)
{
return messageService.add(message);
}
@PUT
@Path("/{messageId}")
public Message updateMessage(@PathParam("messageId")long id,Message message)
{
message.setId(id);
return messageService.updateMessage(message);
}
@GET
@Path("/{messageId}")
@Produces(MediaType.APPLICATION_JSON)
public Message getMessage(@PathParam("messageId")long id)
{
return messageService.getMessage(id);
//return "Got Path Param" + messageId;
}
}
/////////////////////////My Service///////////////////////////////
package org.glassfish.jersey.archetypes.jersey_quickstart_webapp.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.glassfish.jersey.archetypes.jersey_quickstart_webapp.database.DatabaseClass;
import org.glassfish.jersey.archetypes.jersey_quickstart_webapp.model.Message;
public class MessageService {
private Map<Long, Message> messages = DatabaseClass.getMessages();
public MessageService() {
messages.put(1l, new Message(1, "Hello World", "Nishil"));
messages.put(2l, new Message(2, "Hello Jersey", "Nishil"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Message> getAllMessages()
{
messages.values();
return new ArrayList<Message>(messages.values());
}
public Message getMessage(long id)
{
return messages.get(id);
}
public Message add(Message message)
{
message.setId(messages.size()+1);
messages.put(message.getId(), message);
return message;
}
public Message updateMessage(Message message)
{
if(message.getId()<=0)
{
return null;
}
messages.put(message.getId(), message);
return message;
}
public Message removeMessage(long id)
{
return messages.remove(id);
}
}
Solution 1:
You need to set the Content-Type: application/json
header in Postman. I believe it just defaults to text/plain
if you don't set it, hence the "unsupported media type".