错误415不支持的媒体类型:如果是JSON,则POST不会到达REST,但如果是XML则不会 [英] Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML

查看:120
本文介绍了错误415不支持的媒体类型:如果是JSON,则POST不会到达REST,但如果是XML则不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是REST WS的新手,但实际上我没有得到 415不支持的媒体类型

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

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

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.

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

This is my URL: 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;
    }

}


推荐答案

问题出在bean Customer的反序列化中。你的程序知道如何在XML中使用JAXB,就像Daniel正在编写的那样,但很可能不知道如何在JSON中执行它。

The issue is in the deserialization of the bean Customer. Your programs knows how to do it in XML, with JAXB as Daniel is writing, but most likely doesn't know how to do it in JSON.

这里有一个例如与RestEasy的/杰克逊
HTTP:// WWW .mkyong.com / webservices / jax-rs / integrate-jackson-with-resteasy /

Here you have an example with Resteasy/Jackson http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/

与泽西岛相同:
http://www.mkyong.com/webservices/jax-rs / json-example-with-jersey-jackson /

这篇关于错误415不支持的媒体类型:如果是JSON,则POST不会到达REST,但如果是XML则不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆