简体   繁体   中英

How to send JSON data in request body suing apache CXF webclient?

I am using apache cxf webclient to consume a service written in .NET

sample JSON to be sent in request body to a web service

{
   "Conditions":
      [
         {
            "Field":"TextBody",
            "Comparer":"ContainsAny",
            "Values":["stocks","retire"],
            "Proximity":0
         },
         {
            "Field":"SentAt",
            "Comparer":"LessThan",
            "Values":["1331769600"],
            "Proximity":0
         },
      ],
   "Operator":"And",
   "ExpireResultIn":3600
}

Is there any way if I want to submit data from both form and in Json body in one request ? webclient API apache CXF -

web client API doc

WebClient client = WebClient.create("http://mylocalhost.com:8989/CXFTest/cxfws/rest/restservice/json");
 client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);

After this which method and how to use ?

client.form(...form object )

client.post(...JSON string ) 

They have not shared Object of "Conditions" in JSON which I can annotate and pass to post method of client

I got answer here Need to set JSON provider in my case it was jackson

List<Object> providers = new ArrayList<Object>();
providers.add( new JacksonJaxbJsonProvider() );

WebClient client = WebClient.create("http://localhost:8080/poc_restapi_cxf/api", 
                                     providers);
client = client.accept("application/json")
               .type("application/json")
               .path("/order")
               .query("id", "1");

 Order order = client.get(Order.class);
 System.out.println("Order:" + order.getCustomerName());

There is a way to do this using annotations and suited my purpose:

@Post
@Path("mypath/json/whatever")
@Consumes({MediaType.APPLICATION_JSON_VALUE})
public Response postClient(@Context HttpHeaders headers, String input) {
    //Here the String input will be equal to the supplied json. 
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM