How to send JSON data to the request body requesting apache CXF webclient?

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

JSON sample to be sent to the request body of the 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 a way if I want to send data from both forms and to the Json body on one request? Apache CXF web client API -

web client API document

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

After that, what method and how to use?

client.form(...form object )

client.post(...JSON string ) 

They did not share the Conditions object in JSON, which I can annotate and pass to the client’s mail method

+4
source share
2 answers

I got the answer here. I need to install a 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());
+3
source

:

@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. 
    //...
}
0

All Articles