2024年3月21日发(作者:)

OutputStream outputStream = putStream();

(alueAsBytes(entity));

();

BufferedReader reader = new BufferedReader(new InputStreamReader(utStream()));

String output;

("addResource result is : ");

while ((output = ne()) != null) {

(output);

}

("n");

}

public static void getAllResource() throws Exception {

URL url = new URL(REST_API + "/getAllResource");

HttpURLConnection httpURLConnection = (HttpURLConnection) nnection();

uestMethod("GET");

uestProperty("Accept", "application/json");

BufferedReader reader = new BufferedReader(new InputStreamReader(utStream()));

String output;

("getAllResource result is :");

while ((output = ne()) != null) {

(output);

}

("n");

}

}

结果:

addResource result is : {"id":"NO2","name":"Joker","addr":""}

getAllResource result is :[{"id":"NO2","name":"Joker","addr":""}]

(3)HttpClient

package ;

import Mapper;

import Entity;

import sponse;

import ient;

import t;

import st;

import Entity;

import tHttpClient;

import Utils;

/**

* 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();

PersonEntity entity = new PersonEntity("NO2", "Joker", "");

ObjectMapper mapper = new ObjectMapper();

HttpPost request = new HttpPost(REST_API + "/addResource/person");

der("Content-Type", "application/json");

der("Accept", "application/json");

StringEntity requestJson = new StringEntity(alueAsString(entity), "utf-8");

tentType("application/json");

ity(requestJson);

HttpResponse response = e(request);

String json = ng(ity());

("addResource result is : " + json + "n");

}

public static void getAllResource() throws Exception {

HttpClient httpClient = new DefaultHttpClient();

HttpGet request = new HttpGet(REST_API + "/getAllResource");

der("Content-Type", "application/json");

der("Accept", "application/json");

HttpResponse response = e(request);

String json = ng(ity());

("getAllResource result is : " + json + "n");

}

}

结果:

addResource result is : {"id":"NO2","name":"Joker","addr":""}

getAllResource result is : [{"id":"NO2","name":"Joker","addr":""}]

maven:

mponents

httpclient

4.1.2

(4)JAX-RS API

package ;

import Entity;

import ;

import Builder;

import ;

import ype;

import se;

import ption;

/**

* 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 = ent();

ty("Content-Type","xml");

Response response = (REST_API + "/getRandomResource").request().get();

String str = tity();

("getRandomResource result is : " + str + "n");

}

public static void addResource() {

Client client = ent();

PersonEntity entity = new PersonEntity("NO2", "Joker", "");

Response response = (REST_API + "/addResource/person").request().buildPost((entity, ATION_JSON)).invoke();

String str = tity();

("addResource result is : " + str + "n");

}

public static void getAllResource() throws IOException {

Client client = ent();

ty("Content-Type","xml");

Response response = (REST_API + "/getAllResource").request().get();

String str = tity();

("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

package ;

import Mapper;

import Entity;

import ent;

import se;

/**

* 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 = (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 = ("/addResource/person").post(alueAsString(entity), );

String json = tity();

("addResource result is : " + json + "n");

}

public static void getAllResource() {

WebClient client = (REST_API)

.header("Content-Type", "application/json")