|
|
Java client for restful web service using java.net package
In this page you will come to know how to create java client for restful web services using java.net package. We will have
two sections here, the first section talks about how to connect to "GET" request, and the second section shows how to connect to "POST"
type of requests.
We are referring to the previous example as a restful web services.
Java Client for GET Request
package com.java2novice.rest.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class RestJavaNetClient {
public static void main(String a[]){
String url = "http://localhost:8080/RestfulWebServices/order-inventory/order/1016";
HttpURLConnection urlConn = null;
BufferedReader reader = null;
try {
URL urlObj = new URL(url);
urlConn = (HttpURLConnection) urlObj.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setConnectTimeout(5000);
urlConn.setReadTimeout(5000);
urlConn.setRequestProperty("Accept", "application/json");
if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.err.println("Unable to connect to the URL...");
return;
}
System.out.println("Connected to the server...");
InputStream is = urlConn.getInputStream();
reader = new BufferedReader(new InputStreamReader((is)));
System.out.println("Reading data from server...");
String tmpStr = null;
while((tmpStr = reader.readLine()) != null){
System.out.println(tmpStr);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(reader != null) reader.close();
if(urlConn != null) urlConn.disconnect();
} catch(Exception ex){
}
}
}
}
|
The output for above code is:
Output: |
Connected to the server...
Reading data from server...
{"custmer":"Java2Novice","address":"Bangalore","bill-amount":"$2000"}
|
Java Client for POST Request
In this example you will see how to send json input through java client using POST method:
package com.java2novice.rest.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class PostJavaNetClient {
public static void main(String a[]){
String url = "http://localhost:8080/RestfulWebServices/order-inventory/order";
HttpURLConnection urlConn = null;
BufferedReader reader = null;
OutputStream ouputStream = null;
String jsonInput = "{\"custmer\":\"Java2novice\",\"address\":\"Bangalore\","+
"\"bill-amount\":\"$2000\"}";
try {
URL urlObj = new URL(url);
urlConn = (HttpURLConnection) urlObj.openConnection();
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.setConnectTimeout(5000);
urlConn.setReadTimeout(5000);
urlConn.setRequestProperty("Accept", "application/json");
// send json input request
ouputStream = urlConn.getOutputStream();
ouputStream.write(jsonInput.getBytes());
ouputStream.flush();
if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.err.println("Unable to connect to the URL...");
return;
}
System.out.println("Connected to the server...");
InputStream is = urlConn.getInputStream();
reader = new BufferedReader(new InputStreamReader((is)));
String tmpStr = null;
while((tmpStr = reader.readLine()) != null){
System.out.println(tmpStr);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(reader != null) reader.close();
if(urlConn != null) urlConn.disconnect();
} catch(Exception ex){
}
}
}
}
|
The output for above code is:
Output: |
Connected to the server...
Your order is in-progress
|
|
|
|
|
Restful Web Services Examples
- Restful web services using RESTEasy hello world example.
- Restful web services using Jersey hello world example.
- JAX-RS @Path annotation example
- JAX-RS @Path annotation with regular expression match example
- JAX-RS @PathParam annotation example
- JAX-RS @QueryParam and @DefaultValue annotations example
- JAX-RS @MatrixParam annotation example
- How to read multiple values of a query paramter in JAX-RS restful web services?
- How to pass header parameters as method inputs in JAX-RS restful web services?
- How to read header parameters in JAX-RS restful web services?
- JAX-RS @FormParam annotation example
- How to upload file using Jersey restful web services?
- How to download file using java restful web services?
- XML based Restful web service with RESTEasy and JAXB.
- XML based Restful web service with Jersey and JAXB.
- Json based Restful web service with RESTEasy, Jettison and JAXB
- Json based Restful web service with RESTEasy and Jackson
- Json based Restful web service with Jersey and Jackson
- How to input json request with Jersey and Jackson?
- Java client for restful web service using java.net package
- Java client for restful web service using Jersey API
- Java restful webservices with HTTP basic authentication.
|
|
|
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based.
But accessing is difficult with LinkedList. It is slow access. This is
to access any element, you need to navigate through the elements one by
one. But insertion and deletion is much faster with LinkedList, because
if you know the node, just change the pointers before or after nodes.
Insertion and deletion is slow with ArrayList, this is because, during
these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,
an ArrayList having 10 elements, if you are inserting at index 5, then
you need to shift the indexes above 5 to one more.
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author
|