java - 错误 415 不支持的媒体类型 : POST not reaching REST if

我实际上是 REST WS 的新手,但我真的不明白这个 415 Unsupported Media Type

我在 Firefox 上使用 Poster 测试我的 REST,GET 对我来说很好,POST (当它是 application/xml) 但是当我尝试 application/json 它根本没有到达 WS,服务器拒绝它。

这是我的网址:http://localhost:8081/RestDemo/services/customers/add

这是 JSON 我要发送:{"name": "test1", "address":"test2"}

这是我要发送的 XML:

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

这是我的资源类:

@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() + "\"}";
    }

}

编辑 1:

这是我的客户类:

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

}

最佳答案

添加 Content-Type: application/jsonAccept: application/json 在 REST 客户端标题部分中

关于java - 错误 415 不支持的媒体类型 : POST not reaching REST if JSON, 但如果 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11773846/

相关文章:

java - gson.toJson() 抛出 StackOverflowError

c# - 使用 Web API 返回匿名类型

json - ASP.net MVC 返回 JSONP

python - 读取相当大的 JSON 文件

javascript - 排序对象属性和 JSON.stringify

java - jackson + build 者模式?

java - 将 PostgreSQL JSON 列映射到 Hibernate 实体属性

java - 如何使用 Gson 解析 JSON 数组

javascript - 将 JSON 对象转换为 pretty-print 的 JSON 的 An

java - 如何从 Java HTTPResponse 解析 JSON?