RESTful Java client with java.net.URL
来源:互联网 发布:淘宝全民分享怎么设置 编辑:程序博客网 时间:2024/04/29 07:33
In this tutorial, we show you how to create a RESTful Java client with Java build-in HTTP client library. It’s simple to use and good enough to perform basic operations for REST service.
The RESTful services from last “Jackson + JAX-RS” article will be reused, and we will use “java.net.URL
” and “java.net.HttpURLConnection
” to create a simple Java client to send “GET” and “POST” request.
1. GET Request
Review last REST service, return “json” data back to client.
@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; } //...
Java client to send a “GET” request.import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class NetClientGet { // http://localhost:8080/RESTfulExample/json/product/get public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/RESTfulExample/json/product/get"); 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()); } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
Output…
Output from Server ….
{"qty":999,"name":"iPad 3"}
2. POST Request
Review last REST service, accept “json” data and convert it into Product object, via Jackson provider automatically.
@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(); } //...
Java client to send a “POST” request, with json string.import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class NetClientPost { // http://localhost:8080/RESTfulExample/json/product/post public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/RESTfulExample/json/product/post"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); String input = "{\"qty\":100,\"name\":\"iPad 4\"}"; OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
Output…
Output from Server ….
Product created : Product [name=iPad 4, qty=100]
0 0
- RESTful Java client with java.net.URL
- RESTful Java Client With RESTEasy Client Framework
- RESTful Java client with RESTEasy client framework
- RESTful Java client with Apache HttpClient
- RESTful client for java
- Implements a Java FTP client with sun.net.*
- RESTful java with JAX-RS
- java实现客户端Client调用Restful接口
- RESTful Webservices with Java (Jersey / JAX-RS)
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header,Spring RestTemplate在spr
- java.net.url
- java.net.URL例子
- java.net解析URL
- java.net.URL 练习
- java.net.URL
- java.net 类 URL
- java.net.url对象
- java restful
- 第23章 慢查询日志
- 8.5.3 Optimizing InnoDB Read-Only Transactions 优化InnoDB 只读事务
- 技术路线开始记录~
- 华为OJ(汽水瓶)
- uva 811 - The Fortified Forest(暴力+凸包)
- RESTful Java client with java.net.URL
- Android设置虚线、圆角、渐变
- velocity
- HDU4687 (一般图匹配)
- tslib移植
- ural 1109. Conference 匈牙利算法
- 树的基本概述
- 设置多行多列图标的循环体
- 【索引】Geometric Algorithms in 2D::Exercises: Beginner