Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Rest Client

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to make a client for an existing RESTful webservice with method signature: String serviceMethod(String).

It will be great if I get some idea about the Rest client.

Thanks
Joice
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One virtue of RESTful web services is that clients can be very simple and you dont need any special toolkit.

Once you have got the correct URL and HTTP method to address the service you can create a HttpURLConnection to make the request and get the response. See the JavaDocs in the java.net package.

One important point: if your URL starts with "http://" then the URL openConnection returns a HttpURLConnection - a subclass of URLConnection so you need to look at the JavaDocs for both. Setting the request method to "GET" or "POST" uses an HttpURLConnection method.

You might also search for "HttpURLConnection tutorial."

Bill
 
Joice Jose
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bill.

I tried to create the connection as:

URL myURL = new URL("http://localhost/ats/rest/MyService/getData");//MyService is service and getData is method
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();

But it ended up with a java.io.IOException: Server returned HTTP response code: 500

Joice
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking in the HttpURLConnection javadocs we see that a response code of 500 means "HTTP Internal Server Error" - its time to look at the server side logs for more explanation.

From your initial post I thought you were talking about an existing service created by somebody else, now I see you are addressing it as localhost. How was this service created?

Bill
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic