java - 错误 415 不支持的媒体类型 : POST not reaching REST if JSON, 但如果 XML

标签 java json web-services rest post

我实际上是 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 5 授权 header 字符集 ISO-8859-2

java - Jax RS授权

java - equalsIgnoreCase 与 Java 中的 regionMatches。哪个效率高?

java - 如何使用java apache poi api创建Excel列仅允许唯一字符串

javascript - 将带有符号的 ES6 类转换为 JSON

php - Laravel sql搜索json类型列

php - Select2 具有多个值的问题

java - WebSphere 中的多个 Web 容器线程池 - 这可能吗?

java - 按下按钮后,如何将 GUI 驱动程序类中的变量传输到 ActionListener 类?

java - 使用 PHP Soap 服务器和 Java 客户端的 Web 服务