RestTemplateテンプレートオブジェクトパッケージHttpClientのPRC実装
8726 ワード
1.RestTemplateテンプレートの対象紹介
1.1 spring-resttmplate(jar)プロジェクトの作成
1.2依存の追加
1.3 application-server.xml
1.4 restTemplateオブジェクトのpost要求の送信をテストする
1.5 RestTemplateオブジェクトを使用してGet要求を送信する
1.6 RestTemplateオブジェクトを使用したPostリクエストの送信
コードのまとめ:
1.RestTemplateテンプレートの対象紹介
Springはhttpリクエストを送信するためのオブジェクトを提供し、httpclient操作のカプセル化を完了する
1.1 spring-resttmplate(jar)プロジェクトの作成
1.2依存の追加
1.3 application-server.xml
1.4 restTemplateオブジェクトのpost要求の送信をテストする
1.5 RestTemplateオブジェクトを使用してGet要求を送信する
1.6 RestTemplateオブジェクトを使用したPostリクエストの送信
コードのまとめ:
1.1 spring-resttmplate(jar)プロジェクトの作成
1.2依存の追加
1.3 application-server.xml
1.4 restTemplateオブジェクトのpost要求の送信をテストする
1.5 RestTemplateオブジェクトを使用してGet要求を送信する
1.6 RestTemplateオブジェクトを使用したPostリクエストの送信
コードのまとめ:
1.RestTemplateテンプレートの対象紹介
Springはhttpリクエストを送信するためのオブジェクトを提供し、httpclient操作のカプセル化を完了する
1.1 spring-resttmplate(jar)プロジェクトの作成
1.2依存の追加
4.0.0
com.bjsxt.rest
spring-resttemplate
0.0.1-SNAPSHOT
org.springframework
spring-webmvc
4.3.18.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.9.9.3
1.3 application-server.xml
1.4 restTemplateオブジェクトのpost要求の送信をテストする
package com.bjsxt.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.bjsxt.pojo.Orders;
public class RestTemplateApp {
static RestTemplate template;
/***
* RestTemplate
*/
public static void main(String[] args) {
// Spring
ApplicationContext ac
=new ClassPathXmlApplicationContext("application-service.xml");
// RestTemplate
template = (RestTemplate) ac.getBean("template");
sendPost();
}
/***
* RestTemplate Post
*/
public static void sendPost() {
// LinkedMultiValueMap
LinkedMultiValueMap
headers=new LinkedMultiValueMap();
headers.add("Content-Type", "application/x-www-form-urlencoded");
//
LinkedMultiValueMap body=
new LinkedMultiValueMap();
body.add("vid", "12345678");
// HttpEntity ,
HttpEntity>
entity=new HttpEntity<>(body,headers);
String url="http://localhost:8081/loadOrdersList";
// http,post ,
//Orders[] results=template.postForObject(url,entity, Orders[].class);
//List asList = Arrays.asList(results);
// http,post ,
ResponseEntity response
= template.postForEntity(url,entity, Orders[].class);
//
int statusCodeValue = response.getStatusCodeValue();
System.out.println(statusCodeValue);
//
Orders[] results = response.getBody();
for(Orders orders:results) {
System.out.println(orders.getId()+"\t"+orders.getRemark()
+"\t"+orders.getOdate()+"\t"+orders.getVip());
}
}
/***
* RestTemplate Get
*/
/***
* RestTemplate Post , json
*/
}
1.5 RestTemplateオブジェクトを使用してGet要求を送信する
/***
* RestTemplate Get
*/
public static void sendGet() {
String url="http://localhost:8081/loadOrders?id=888888";
/***
* ResponseEntity
*/
//ResponseEntity entity = template.getForEntity(url, Orders.class);
//Orders orders = entity.getBody();
//
Orders orders = template.getForObject(url, Orders.class);
System.out.println(orders.getId()+"\t"+orders.getRemark()
+"\t"+orders.getVip()+"\t"+orders.getOdate());
}
1.6 RestTemplateオブジェクトを使用したPostリクエストの送信
/***
* RestTemplate Post , json
*/
public static void sendPostObject() {
// Orders
Orders orders=new Orders();
orders.setId(77777777);
orders.setRemark(" ....");
orders.setOdate("2010-10-10");
orders.setVip(99999);
//
HttpHeaders header=new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON_UTF8);
// javabean json
String jsonString = JSON.toJSONString(orders);
//
HttpEntity entity=new HttpEntity(jsonString,header);
String url="http://localhost:8081/saveOrders";
// post
Map result = template.postForObject(url, entity, Map.class);
System.out.println(result);
}
コードのまとめ:
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.bjsxt.pojo.Orders;
import com.fasterxml.jackson.databind.util.JSONPObject;
public class RestTemplateTest {
static RestTemplate template;
public static void main(String[] args) {
// Spring
ApplicationContext ac = new ClassPathXmlApplicationContext("application-service.xml");
// RestTemplate
template = (RestTemplate) ac.getBean("template");
sendObject();
}
// post
public static void sendPost() {
// LinkedMultiValueMap
LinkedMultiValueMap headers = new LinkedMultiValueMap();
headers.add("Content-Type", "application/x-www-form-urlencoded");
//
LinkedMultiValueMap body = new LinkedMultiValueMap();
body.add("vid", "12345678");
// HttpEntity ,
HttpEntity> entity = new HttpEntity<>(body, headers);
String url = "http://localhost:8081/loadOrdersList";
// http,post ,
// Orders[] results=template.postForObject(url,entity, Orders[].class);
// List asList = Arrays.asList(results);
// http,post ,
ResponseEntity response = template.postForEntity(url, entity, Orders[].class);
//
int statusCodeValue = response.getStatusCodeValue();
System.out.println(statusCodeValue);
//
Orders[] results = response.getBody();
for (Orders orders : results) {
System.out.println(
orders.getId() + "\t" + orders.getRemark() + "\t" + orders.getOdate() + "\t" + orders.getVip());
}
}
// get
public static void sendGet() {
String url = "http://localhost:8081/selectOrder?id=88888";
ResponseEntity response = template.getForEntity(url, Orders.class);
Orders body = response.getBody();
System.out.println(body);
}
// json
public static void sendObject() {
// Orders
Orders orders = new Orders();
orders.setId(77777777);
orders.setRemark(" ....");
orders.setOdate("2010-10-10");
orders.setVip(99999);
String url="http://localhost:8081/addOrders";
// json
String jsonString = JSON.toJSONString(orders);
// json'
HttpHeaders header=new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON_UTF8);
// ( 、 )
HttpEntityrequest=new HttpEntity<>(jsonString,header);
Mapmap
= template.postForObject(url, request, Map.class);
System.out.println(map);
}
}