How to send the JSON data in rest web services?

假如想象 提交于 2019-12-20 07:19:46

问题


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 integer only except product Unit value. Now, I want to send these values into rest web services. Could you please give any samples or any valuable suggestions?


回答1:


Since you have tagged this with the Worklight tag, I'm going to assume you meant to ask how to send json data from a worklight client to an external REST service. In order to do this in Worklight, you need to use a Worklight HTTP adapter. See the documentation here: http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v600/04_02_HTTP_adapter_-_Communicating_with_HTTP_back-end_systems.pdf

After creating the Worklight adapter, you can then send your JSON data from the client like this:

/**********************************************************************************
*                           Http Adapter call
**********************************************************************************/
function callAdapter(){

    var myJSONObject = {
        productId: 123, 
        storeId: 123, 
        price: 342, 
        productUnit: "myUnit",
        quantity: 4
    };

    var invocationData = {
            adapter : 'MyHttpAdapter',
            procedure : 'myAdapterProcedure',
            parameters : [myJSONObject]
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : success,
        onFailure : failure
    });
}

function success(response){
    console.log("adapter Success");
    console.log(response);
}

function failure(response){
    console.log("adapter Failure");
    console.log(response);
}



回答2:


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.



来源:https://stackoverflow.com/questions/18781775/how-to-send-the-json-data-in-rest-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!