提问者:小点点

错误415不支持的媒体类型:如果JSON,POST未达到REST,但如果XML,则达到REST


我实际上是REST WS的新手,但我真的不了解这个415不支持的媒体类型

我正在Firefox上用Poster测试我的REST,GET对我来说很好,POST(当它是应用程序/xml时)也很好,但当我尝试application/json它根本无法到达WS时,服务器会拒绝它。

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

这是我发送的< code > JSON :< code > { " name ":" test1 "," address" :"test2"}

这是我发送的< code>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;
    }

}

共3个答案

匿名用户

在REST客户端头部分添加< code > Content-Type:application/JSON 和< code > Accept:application/JSON

匿名用户

问题在于bean Customer的反序列化。您的程序知道如何使用XML(正如Daniel所写的那样使用JAXB)实现,但很可能不知道如何使用JSON实现。

这里有一个Resteasy/Jackson的例子http://www . mkyong . com/web services/jax-RS/integrate-Jackson-with-rest easy/

泽西岛也一样:http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

匿名用户

以防万一这对其他人有帮助,以下是我的轶事:

我发现这个线程是因为我在使用Postman将测试数据发送到我的REST简单服务器时遇到的问题,在进行重大代码更改后,我只收到415个不支持的媒体类型错误。

长话短说,我把所有东西都撕了,最后我试着运行我知道有效的琐碎文件上传例子;它没有。这时我意识到问题出在我的邮递员请求上。我通常不发送任何特殊的头,但是在之前的测试中,我添加了一个“Content-Type”:“application/JSON”头。当然,我试图上传“多部分/表单数据”移除它解决了我的问题。

道德:在炸毁你的世界之前检查你的标题。;)