Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML

mzereba picture mzereba · Aug 2, 2012 · Viewed 394.5k times · Source

I am actually new to REST WS but really I don't get this 415 Unsupported Media Type.

I am testing my REST with Poster on Firefox and the GET works fine for me, also the POST (when it's a application/xml) but when I try application/json it doesn't not reach the WS at all, the server rejects it.

This is my URL: http:// localhost:8081/RestDemo/services/customers/add

This is JSON I'm sending: {"name": "test1", "address" :"test2"}

This is XML I'm sending:

<customer>
    <name>test1</name>
    <address>test2</address>
</customer>

and this is my Resource class:

@Produces("application/xml")
@Path("customers")
@Singleton
@XmlRootElement(name = "customers")
public class CustomerResource {

    private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();

    public  CustomerResource() {
        // hardcode a single customer into the database for demonstration
        // purposes
        Customer customer = new Customer();
        customer.setName("Harold Abernathy");
        customer.setAddress("Sheffield, UK");
        addCustomer(customer);
    }

    @GET
    @XmlElement(name = "customer")
    public List<Customer> getCustomers() {
        List<Customer> customers = new ArrayList<Customer>();
        customers.addAll(customerMap.values());
        return customers;
    }

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public String getCustomer(@PathParam("id") int cId) {
        Customer customer = customerMap.get(cId); 
        return  "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";

    }

    @POST
    @Path("/add")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public String addCustomer(Customer customer) {
         //insert 
         int id = customerMap.size();
         customer.setId(id);
         customerMap.put(id, customer);
         //get inserted
         Customer result = customerMap.get(id);

         return  "{\"id\": \" " + result.getId() + " \", \"name\": \" " + result.getName() + " \", \"address\": \"" + result.getAddress() + "\"}";
    }

}

EDIT 1:

This is my Customer class:

@XmlRootElement 
public class Customer implements Serializable {

    private int id;
    private String name;
    private String address;

    public Customer() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

Answer

Anup Awasthi picture Anup Awasthi · Nov 21, 2012

Add Content-Type: application/json and Accept: application/json in REST Client header section