aspose file tools*
The moose likes Web Services and the fly likes REST tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "REST tutorial" Watch "REST tutorial" New topic
Author

REST tutorial

Ong Vua
Ranch Hand

Joined: Jan 24, 2008
Posts: 59
I need to call a REST service and I want to use Java JDK.

Below is login info from the 3rd party vendor given to me.

URL: http://servername/rest/user/login
HTTP Request Type: POST
Parameters: username, password
Cookie Required: False

The response will be a JSON-encoded object.

How do I call this REST service and parse the response back? Do you have any tutorial that I can review? I'm new to REST.

Thanks.
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
A simple java call with java.net.URL and HttpURLConnection will do (or) use httpclient jar. Example Rest Java Client Example.
To parse jason objects too there are bunch of thrid party apis like google-gson or you can write your own simple parser.

Here is a good tutorial to understand rest : REST Tutorial , it's easy then you think.


Thanks,
Suri
Ong Vua
Ranch Hand

Joined: Jan 24, 2008
Posts: 59
Below is my code

String urlParameters = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("password1", "UTF-8");

URL url = new URL("http://servername/rest/user/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
line = rd.readLine();
while (line != null) {
System.out.println(line);
line = rd.readLine();
}

wr.flush();
wr.close();
connection.disconnect();

But I got the below errors back

java.io.IOException: Server returned HTTP response code: 401 for URL: http://servername/rest/user/login
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at client.Class1.login(Class1.java:31)
at client.Class1.main(Class1.java:130)


Do you have any idea?

Thanks
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42

401 error is generally due to authentication failure, looks like the url you are trying to access needs basic authentication to access the url. Find out the credentials and see below code to send those in http header.

Ong Vua
Ranch Hand

Joined: Jan 24, 2008
Posts: 59
Thanks. I got past the security thing and now I got the 406 error. Below is my code. For some reason, it does not recognize the JSON object returned. Do you have any ideas? Thanks

Ong Vua
Ranch Hand

Joined: Jan 24, 2008
Posts: 59
I got the below error message back. Do you have any ideas?

HTTP/1.1 406 Not Acceptable: Unsupported request content type application/x-www-form-urlencoded


Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

hey ong i'm also trying to implement the same please guide how to approach this problem.


| B.E IT | SCJP 6.0 98 % |
Surender Suri
Ranch Hand

Joined: May 28, 2010
Posts: 42
Can you try with "text/xml" instead of "application/x-www-form-urlencoded" in below line.



Also, here is a nice article on these issue. Hope it helps : RESTful HTTP in practice
Ong Vua
Ranch Hand

Joined: Jan 24, 2008
Posts: 59
Thanks. I tried changing it but still got 406 error. The application/x-www-form-urlencoded is for POST since I'm posting the username and password parameters in the form.

Thanks for the article. I read that article before also and everything I put in my code follows it but still no luck
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: REST tutorial