RESTful Java Client With RESTEasy Client Framework
来源:互联网 发布:js可关闭全屏广告代码 编辑:程序博客网 时间:2024/06/04 17:44
This tutorial show you how to create a RESTful Java client with RESTEasy client framework, to perform “GET” and “POST” requests to REST service that created in last “Jackson + JAX-RS” tutorial.
1. RESTEasy Client Framework
RESTEasy client framework is included in RESTEasy core module, so, you just need to declares the “resteasy-jaxrs.jar” in your pom.xml
file.
File : pom.xml
<dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jaxrs</artifactId><version>2.2.1.GA</version></dependency>
2. GET Request
Review last REST service.
@Path("/json/product")public class JSONService { @GET@Path("/get")@Produces("application/json")public Product getProductInJSON() { Product product = new Product();product.setName("iPad 3");product.setQty(999); return product; }//...
RESTEasy client to send a “GET” request.
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStreamReader;import org.apache.http.client.ClientProtocolException;import org.jboss.resteasy.client.ClientRequest;import org.jboss.resteasy.client.ClientResponse; public class RESTEasyClientGet { public static void main(String[] args) { try { ClientRequest request = new ClientRequest("http://localhost:8080/RESTfulExample/json/product/get");request.accept("application/json");ClientResponse<String> response = request.get(String.class); if (response.getStatus() != 200) {throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus());} BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes()))); String output;System.out.println("Output from Server .... \n");while ((output = br.readLine()) != null) {System.out.println(output);} } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
Output…
Output from Server .... {"qty":999,"name":"iPad 3"}
3. POST Request
Review last REST service also.
@Path("/json/product")public class JSONService { @POST@Path("/post")@Consumes("application/json")public Response createProductInJSON(Product product) { String result = "Product created : " + product;return Response.status(201).entity(result).build(); }//...
RESTEasy client to send a “POST” request.
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import org.jboss.resteasy.client.ClientRequest;import org.jboss.resteasy.client.ClientResponse; public class RESTEasyClientPost { public static void main(String[] args) { try { ClientRequest request = new ClientRequest("http://localhost:8080/RESTfulExample/json/product/post");request.accept("application/json"); String input = "{\"qty\":100,\"name\":\"iPad 4\"}";request.body("application/json", input); ClientResponse<String> response = request.post(String.class); if (response.getStatus() != 201) {throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus());} BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes()))); String output;System.out.println("Output from Server .... \n");while ((output = br.readLine()) != null) {System.out.println(output);} } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
Output…
Output from Server .... Product created : Product [name=iPad 4, qty=100]
Download Source Code
Download it – RESTful-Java-Client-RESTEasyt-Example.zip (9 KB)
References
- Jackson Official Website
- RESTful Java client with java.net.URL
- RESTful Java client with Apache HttpClient
0 0
- RESTful Java Client With RESTEasy Client Framework
- RESTful Java client with RESTEasy client framework
- RESTful Java client with java.net.URL
- RESTful Java client with Apache HttpClient
- RESTful client for java
- Resteasy Client API
- restful client
- java实现客户端Client调用Restful接口
- RestFul WCF JSON Service with client and on Mozilla Firefox –REST Client
- jboss resteasy client ProxyFactory 自动化测试架构
- RESTEasy client for consuming REST APIs
- NET Framework Client Profile
- .NET Framework Client Profile
- java client
- Advanced Rest Client调试RESTFul
- Restful API Client客户端身份验证
- client
- client
- C语言封装的轻量线程环境 Protothreads
- jsp下页面跳转的几种方法小结
- 漏洞_@2014-03-18
- 模板引擎-NVelocity
- 详解arm中entry的使用
- RESTful Java Client With RESTEasy Client Framework
- memcache研究笔记 之 hashtable
- 项目4--扩展1
- 死亡洞穴
- 改善程序
- 图解Protothreads
- C++多态
- 利用Protothread实现实时多任务系统
- 建模工具UML需求分析实例解析