How to send the JSON data in rest web services?

前端 未结 2 634
攒了一身酷
攒了一身酷 2021-01-28 05:37

How to send the JSON data in rest web services? I have a json object which contains product Id,store Id,price,product Unit,quantity values.Here All the values are integ

2条回答
  •  难免孤独
    2021-01-28 06:05

    Your JSON input:

    {
      "productId": "p123",
      "storeId": "s456",
      "price": 12.34,
      "productUnit": "u789",
      "quantity": 42
    }
    

    The JAXB class:

    @XmlRootElement
    public class MyJaxbBean {
        public String productId;
        public String storeId;
        public double price;
        public String productUnit;
        public int quantity;
    
        public MyJaxbBean() {} // JAXB needs this
    
        public MyJaxbBean(String productId, String storeId, double price, String productUnit, int quantity) {
            // set members
        }
    }
    

    The JAX-RS method.

    @PUT
    @Consumes("application/json")
    public Response putMyBean(MyJaxbBean theInput) {
        // Do something with theInput
    
        return Response.created().build();
    }
    

    See the documentation of Jersey (the RI for JAX-RS) for details.

提交回复
热议问题