We help IT Professionals succeed at work.

How to call a Restful webservice in JAVA

SiemensSEN
SiemensSEN asked
on
862 Views
Last Modified: 2013-12-11
Hello,
 How do you call a RestFul WebService from a Java Application.  I need to call the URL and Post some data to it and retrive the response.

I was told that the service is written in JAVA and uses the Spring framework

Thanks for your help
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you .

I really will like to use spring. This RestAPI is simple now  but it will get more complicated in future releases.

I will read the links you sent me. However, if it would be very helpful  if you can provide any additional information. As always, I will Google to get more info

Thank you

Author

Commented:
As an example.. I need to post the information below to the this rest URI
http://127.0.0.1:/api/data/add


{
        "message" => "[Wed Jun 26 22:13:22 2013] [client 10.10.10.100] PHP Fatal   ...."
        ,"@timestamp" => "2013-06-27T02:13:22.000Z"
        ,"m_level"        => "Info"
        ,"m_cat"           => "THIRD_PARTY"
        ,"m_cor"           => "None"
        ,"m_tenant"      => "xxxxx"
        ,"m_timezone" => "EDT"
        ,"m_tzoffset"    => "-04:00"
        ,"m_host"         =>"cassandra-03.boca.us"
        ,"m_path"         => "/var/log/messages"
        ,"m_type"         => "linux-syslog"
}

I already constructed  the json object.
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you very much for the help. I will have a look at the RestTemplate and try to understand it.

However, I have to get a demo working this week. So, I will try use the HttpClient for now and later convert to REST.  Here is  my code snippet .. am I using the library correctly .  I will pass the JSON string and the URL to the class

public class Alarms 
{
     String url;
     public Alarms(String url)
     {
          this.url = url;
     }
     public void callRESTAPI(String jsonstring) throws IOException
     {
         CloseableHttpResponse response=null;
         CloseableHttpClient httpclient = HttpClients.createDefault();
         HttpClient c = new HttpClient();
         HttpPost p = new HttpPost(this.url);
         p.setEntity(new StringEntity(jsonstring,ContentType.create("application/json")));
         response = httpclient.execute(p);		
         System.out.println(response.getStatusLine());
                response.close();
}

Open in new window

CERTIFIED EXPERT

Commented:
I haven't tried running your code - but it looks pretty good.

Fortunately httpclient is so widely used there's lots of examples on the web.  If you run into trouble a quick google search will show you lots of examples.

E.g. There's a whole tutorial here:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html

and this part has some more POST examples:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e171

Doug
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
And you can probably delete line 12 from the above code snippet. It's not doing anything useful.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.