Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume REST in Java [duplicate]

Using Java tools,

wscompile for RPC wsimport for Document etc.. 

I can use WSDL to generate the stub and Classes required to hit the SOAP Web Service.

But I have no idea how I can do the same in REST. How can I get the Java classes required for hitting the REST Web Service. What is the way to hit the service anyway?

Can anyone show me the way?

like image 368
Mawia Avatar asked Oct 16 '12 13:10

Mawia


People also ask

How do you consume a REST webservice in Java?

A REST Service in Java EE can be created using JAX-RS. The contents of such service can be consumed using ordinary HTTP requests to a URL. URLs are typically kept simple and have a logical pattern, so it's easy to type them manually in e.g. a browser.

What is consume in REST?

Similarly, the act of consuming or using a REST API means to eat it all up. In context, it means to eat it, swallow it, and digest it — leaving any others in the pile exposed.


1 Answers

Working example, try this:

package restclient; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;  public class NetClientGet {     public static void main(String[] args) {         try {              URL url = new URL("http://localhost:3002/RestWebserviceDemo/rest/json/product/dynamicData?size=5");//your url i.e fetch data from .             HttpURLConnection conn = (HttpURLConnection) url.openConnection();             conn.setRequestMethod("GET");             conn.setRequestProperty("Accept", "application/json");             if (conn.getResponseCode() != 200) {                 throw new RuntimeException("Failed : HTTP Error code : "                         + conn.getResponseCode());             }             InputStreamReader in = new InputStreamReader(conn.getInputStream());             BufferedReader br = new BufferedReader(in);             String output;             while ((output = br.readLine()) != null) {                 System.out.println(output);             }             conn.disconnect();          } catch (Exception e) {             System.out.println("Exception in NetClientGet:- " + e);         }     } } 
like image 120
MAnoj Sarnaik Avatar answered Sep 21 '22 09:09

MAnoj Sarnaik