Restful接⼝调⽤⽅法超详细总结
基于发布的Restful服务,下⾯总结⼏种常⽤的调⽤⽅法。
(1)Jersey API
stful.client;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
ity.PersonEntity;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javax.MediaType;
/
**
* Created by XuHui on 2017/8/7.
*/
public class JerseyClient {
private static String REST_API = "localhost:8080/jerseyDemo/rest/JerseyService";
public static void main(String[] args) throws Exception {
getRandomResource();
addResource();
getAllResource();
}
public static void getRandomResource() {
Client client = ate();
WebResource webResource = source(REST_API + "/getRandomResource");
ClientResponse response = pe(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
String str = Entity(String.class);
System.out.print("getRandomResource result is : " + str + "\n");
}
public static void addResource() throws JsonProcessingException {
Client client = ate();
WebResource webResource = source(REST_API + "/addResource/person");
ObjectMapper mapper = new ObjectMapper();
PersonEntity entity = new PersonEntity("NO2", "Joker", "");
ClientResponse response = pe(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, mapper.writeValueAsString(entity));        System.out.print("addResource result is : " + Entity(String.class) + "\n");
}
public static void getAllResource() {
Client client = ate();
WebResource webResource = source(REST_API + "/getAllResource");
ClientResponse response = pe(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
String str = Entity(String.class);
System.out.print("getAllResource result is : " + str + "\n");
}
}
结果:
getRandomResource result is : {"id":"NO1","name":"Joker","addr":"/"}
addResource result is : {"id":"NO2","name":"Joker","addr":""}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":""}]
(2)HttpURLConnection
stful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
ity.PersonEntity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.HttpURLConnection;
import java.URL;
/**
* Created by XuHui on 2017/8/7.
*/
public class HttpURLClient {
private static String REST_API = "localhost:8080/jerseyDemo/rest/JerseyService";
public static void main(String[] args) throws Exception {
addResource();
getAllResource();
}
public static void addResource() throws Exception {
ObjectMapper mapper = new ObjectMapper();
URL url = new URL(REST_API + "/addResource/person");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
PersonEntity entity = new PersonEntity("NO2", "Joker", "");
OutputStream outputStream = OutputStream();
outputStream.write(mapper.writeValueAsBytes(entity));
outputStream.flush();
BufferedReader reader = new BufferedReader(new InputStream()));        String output;
System.out.print("addResource result is : ");
while ((output = adLine()) != null) {
System.out.print(output);
}
System.out.print("\n");
}
public static void getAllResource() throws Exception {
URL url = new URL(REST_API + "/getAllResource");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Accept", "application/json");
BufferedReader reader = new BufferedReader(new InputStream()));        String output;
System.out.print("getAllResource result is :");
while ((output = adLine()) != null) {
System.out.print(output);
}
System.out.print("\n");
}
}
结果:
addResource result is : {"id":"NO2","name":"Joker","addr":""}
getAllResource result is :[{"id":"NO2","name":"Joker","addr":""}]
(3)HttpClient
stful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
ity.PersonEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.hods.HttpGet;
import org.apache.hods.HttpPost;
import org.ity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* Created by XuHui on 2017/8/7.
*/
public class RestfulHttpClient {
private static String REST_API = "localhost:8080/jerseyDemo/rest/JerseyService";
public static void main(String[] args) throws Exception {
addResource();
getAllResource();
}
public static void addResource() throws Exception {
HttpClient httpClient = new DefaultHttpClient();restful接口调用实例
PersonEntity entity = new PersonEntity("NO2", "Joker", "");
ObjectMapper mapper = new ObjectMapper();
HttpPost request = new HttpPost(REST_API + "/addResource/person");
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
StringEntity requestJson = new StringEntity(mapper.writeValueAsString(entity), "utf-8");
requestJson.setContentType("application/json");
request.setEntity(requestJson);
HttpResponse response = ute(request);
String json = Entity());
System.out.print("addResource result is : " + json + "\n");
}
public static void getAllResource() throws Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(REST_API + "/getAllResource");
request.setHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
HttpResponse response = ute(request);
String json = Entity());
System.out.print("getAllResource result is : " + json + "\n");
}
}
结果:
addResource result is : {"id":"NO2","name":"Joker","addr":""}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":""}]
maven:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
(4)JAX-RS API
stful.client;
ity.PersonEntity;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.MediaType;
import javax.Response;
import java.io.IOException;
/**
* Created by XuHui on 2017/7/27.
*/
public class RestfulClient {
private static String REST_API = "localhost:8080/jerseyDemo/rest/JerseyService";
public static void main(String[] args) throws Exception {
getRandomResource();
addResource();
getAllResource();
}
public static void getRandomResource() throws IOException {
Client client = wClient();
client.property("Content-Type","xml");
Response response = client.target(REST_API + "/getRandomResource").request().get();
String str = adEntity(String.class);
System.out.print("getRandomResource result is : " + str + "\n");
}
public static void addResource() {
Client client = wClient();
PersonEntity entity = new PersonEntity("NO2", "Joker", "");
Response response = client.target(REST_API + "/addResource/person").request().ity(entity, MediaType.APPLICATION_JSON)).invoke();        String str  = adEntity(String.class);
System.out.print("addResource result is : " + str + "\n");
}
public static void getAllResource() throws IOException {
Client client = wClient();
client.property("Content-Type","xml");
Response response = client.target(REST_API + "/getAllResource").request().get();
String str = adEntity(String.class);
System.out.print("getAllResource result is : " + str + "\n");
}
}
结果:
getRandomResource result is : {"id":"NO1","name":"Joker","addr":"/"}
addResource result is : {"id":"NO2","name":"Joker","addr":""}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":""}]
(5)webClient
stful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
ity.PersonEntity;
import f.jaxrs.client.WebClient;
import javax.Response;
/**
* Created by XuHui on 2017/8/7.
*/
public class RestfulWebClient {
private static String REST_API = "localhost:8080/jerseyDemo/rest/JerseyService";
public static void main(String[] args) throws Exception {
addResource();
getAllResource();
}
public static void addResource() throws Exception {
ObjectMapper mapper = new ObjectMapper();
WebClient client = ate(REST_API)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.
encoding("UTF-8")
.acceptEncoding("UTF-8");
PersonEntity entity = new PersonEntity("NO2", "Joker", "");
Response response = client.path("/addResource/person").post(mapper.writeValueAsString(entity), Response.class);
String json = adEntity(String.class);
System.out.print("addResource result is : " + json + "\n");
}
public static void getAllResource() {
WebClient client = ate(REST_API)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.encoding("UTF-8")
.acceptEncoding("UTF-8");
Response response = client.path("/getAllResource").get();
String json = adEntity(String.class);
System.out.print("getAllResource result is : " + json + "\n");
}
}
结果:
addResource result is : {"id":"NO2","name":"Joker","addr":""} getAllResource result is : [{"id":"NO2","name":"Joker","addr":""}
maven:
<dependency>
<groupId>f</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.7.0</version>
</dependency>
注:该jar包引⼊和jersey包引⼊有冲突,不能在⼀个⼯程中同时引⽤。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。