How to consume REST in Java

2019-01-10 09:39发布

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?

10条回答
Rolldiameter
2楼-- · 2019-01-10 10:15

You can able to consume a Restful Web service in Spring using RestTemplate.class.

Example :

public class Application {

    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> call= restTemplate.getForEntity("http://localhost:8080/SpringExample/hello");
        System.out.println(call.getBody())
    }

}

Reference

查看更多
家丑人穷心不美
3楼-- · 2019-01-10 10:16

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);
        }
    }
}
查看更多
Root(大扎)
4楼-- · 2019-01-10 10:16

Its just a 2 line of code.

import org.springframework.web.client.RestTemplate;

RestTemplate restTemplate = new RestTemplate();
YourBean obj = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", YourBean.class);

Ref. Spring.io consuming-rest

查看更多
爷的心禁止访问
5楼-- · 2019-01-10 10:17

As others have said, you can do it using the lower level HTTP API, or you can use the higher level JAXRS APIs to consume a service as JSON. For example:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://host:8080/context/rest/method");
JsonArray response = target.request(MediaType.APPLICATION_JSON).get(JsonArray.class);
查看更多
Fickle 薄情
6楼-- · 2019-01-10 10:18

From your question its not clear whether you are using any frameworks.For REST you will be getting an WADL & Apache CXF recently added support for WADL-first development of REST services.Please go through http://cxf.apache.org/docs/index.html

查看更多
来,给爷笑一个
7楼-- · 2019-01-10 10:19

Look at Jersey. Again, REST is all about the data. And a tutorial here

查看更多
登录 后发表回答