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; } }
Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.
The Best Answer is You need to set the content-type in postman as JSON (application/json). Go to the body inside your POST request, there you will find the raw option. Right next to it, there will be a drop down, select JSON (application. json).
Http 415 Media Unsupported is responded back only when the content type header you are providing is not supported by the application. With POSTMAN, the Content-type header you are sending is Content type 'multipart/form-data not application/json .
Add Content-Type: application/json
and Accept: application/json
in REST Client header section
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With