我对REST WS其实是个新手,但我真的不懂这个415 Unsupported Media Type.

我在 firefox 上用Poster测试我的睡觉,GET型对我来说很好,POST型(当它是application/xml时)也是如此,但当我try application/json型时,它根本达不到WS,服务器拒绝了它.

This is my URL: http:// localhost:8081/RestDemo/services/customers/add

This is JSON I'm sending: {"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() + "\"}";
    }

}

EDIT 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客户的反序列化.正如Daniel所写,您的程序知道如何在XML中使用JAXB,但很可能不知道如何在JSON中使用JAXB.

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

The same with Jersey: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

Json相关问答推荐

JOLT转换,将属性复制到同级别的dict中

如何让jq输出长数据集?

如何在数组抖动中按值分组

我如何知道TJSONNumber是double还是double?

使用 jq 如何更改键的值?

根据值过滤输入的JSON并使用它准备预期的输出

匹配来自不同数组的值

使用 jq 将消息转换为数组

通过 xslt 将内部 json 转换为 xml 时遇到问题

PowerShell:如何将哈希表输出为 json 并使用 foreach 循环将其存储在变量中?

为什么我不能在 C# 中引用 System.Runtime.Serialization.Json

如何将 LinkedHashMap 转换为自定义 java 对象?

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

在 Rails 3 中处理 JS/ERB 模板中的 JSON

如何在 swift 2 中获取 Alamofire.request().responseJSON 的结果值?

使用 jq 从 bash 中管道分隔的键和值创建 JSON

waitUntilAllTask​​sAreFinished 错误 Swift

未调用 npm package.json 脚本

确保数组中的项目属性在 Json Schema 中是唯一的?

在 .NET 中缩小缩进的 JSON 字符串