File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

REST tutorial

 
Ong Vua
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ong Vua
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ong i'm also trying to implement the same please guide how to approach this problem.
 
Surender Suri
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic