Home Changing normal RESTful client code to make it use RestTemplate
Reply: 0

Changing normal RESTful client code to make it use RestTemplate

user1821 Published in May 20, 2018, 10:12 am

I have written a normal client code which hits a URL (shown here as URL) and gets a response back. I'm setting the webResource.type() as "application/XML".

The requestString contains XML data in String form.

Here is my code:

 processPayment(String requestString) throws InterruptedException {
    Client client = null;
                    WebResource webResource  = null;
                    ClientResponse response =null;
                    String results = null; 

                        client = Client.create();       
                        webResource = client.resource(URL);

                        response = webResource.type("application/xml")
                           .post(ClientResponse.class, requestString);

                        if (response.getStatus() != 200) {

                              webResource = null;

                            throw new RuntimeException(" request failed with the HTTP Status: "
                                 + response.getStatus() + ". Fallback initiated.");


                        results = response.getEntity(String.class);

                  } finally {
                      webResource = null;

                    return results;

I want to change this such that it uses one of the RestTemplate POST methods (postForObject, postForEntity etc.).

It also my understanding that with a RestTemplate I will not have to explicitly check for a successful response code as the RestTemplate will handle that for me.

Any ideas, resources, code or any form of help is welcome.

Thank You.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302284 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO