How to consume REST in Java

Mawia :

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?

MAnoj Sarnaik :

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);
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to consume rest services on desktop application with Java?

Java Springboot consume rest api map to a model

Consume Zimbra Get Calendar REST Api in Java

How to consume an internal REST service with a proxy approach?

How to consume a Reactive Spring Rest API with WebClient

How to consume Rest Web service in c#

How to consume REST API with Angular2?

How to consume Azure devops REST API in angular

How to consume rest api in C#

How to consume a rest web service in phonegap for android?

How to declare and consume events in Java

How to consume a json array in java?

Consume java spring rest api and insert records to mysql table

Getting Bad Request while trying to consume REST API in Java SpringBoot

EnergyStar Portfolio Manager - Java Client to consume their REST API with Authentication

How can i consume this webservice with Java?

How to package and consume an existing Java library with OSGI

How to consume json parameter in java restful service

How to easily consume a Kotlin channel producer in Java?

How to get kafka consume lag in java program

django rest_framework with basic auth, how to consume api with django

How to Consume REST Api and then Display Top Stories as JSON

How to consume a REST API properly on a multi page website

Spark SQL: How to consume json data from a REST service as DataFrame

How to programmatically consume a file from a Rest API using Spring?

How to POST to a rest api and consume the non ending stream in c#?

How consume REST service with complex type in C#?

How to avoid hitting REST Authentication API everytime to consume actual Service

How to consume non rest wcf service using Jquery?