如何在Java中使用REST [英] How to consume REST in Java
本文介绍了如何在Java中使用REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Java工具,
wscompile for RPC
wsimport for Document
etc..
我可以使用WSDL生成访问SOAP Web服务所需的存根和类。
I can use WSDL to generate the stub and Classes required to hit the SOAP Web Service.
但我不知道如何在REST中做同样的事情。
如何获得访问REST Web服务所需的Java类。
无论如何,点击这项服务的方法是什么?
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?
有人能指路吗?
推荐答案
您可以使用 HttpURLConnection
。
下面是使用Java SE API(包括JAXB)调用RESTful服务的示例:
You can use HttpURLConnection
.
Below is an example of calling a RESTful service using the Java SE APIs including JAXB:
String uri =
"http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
(Customer) jc.createUnmarshaller().unmarshal(xml);
connection.disconnect();
您可以找到完整的示例这里
You can find the complete example here
这篇关于如何在Java中使用REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文