RestTemplate 简单使用(笔记)
get
public static void get(){
RestTemplate restTemplate = new RestTemplate();
//不带参数get
Notice notice = restTemplate.getForObject("http://localhost:8088/rest/notice",Notice.class);
System.out.println(notice.toString());
//带参数get (占位符)
notice = restTemplate.getForObject("http://localhost:8088/rest/{1}/{2}",Notice.class,"hello",5);
System.out.println(notice.toString());
//带参数get (map)
Map<String,String> map = new HashMap();
map.put("start1","6");
map.put("page2","5");
notice = restTemplate.getForObject("http://localhost:8088/rest/page?start={start1}&page={page2}",Notice.class,map);
System.out.println(notice.toString());
}
/**
* 只是返回参数和get不一样
*/
public static void entity(){
RestTemplate restTemplate = new RestTemplate();
//不带参数get
ResponseEntity<Notice> notice = restTemplate.getForEntity("http://localhost:8088/rest/notice",Notice.class);
System.out.println(notice.getBody().toString());
//带参数get (占位符)
notice = restTemplate.getForEntity("http://localhost:8088/rest/{1}/{2}",Notice.class,"hello",5);
System.out.println(notice.getBody().toString());
//带参数get (map)
Map<String,String> map = new HashMap();
map.put("start1","6");
map.put("page2","5");
notice = restTemplate.getForEntity("http://localhost:8088/rest/page?start={start1}&page={page2}",Notice.class,map);
System.out.println(notice.getBody().toString());
}
post
/**
* post
*/
public static void post(){
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> requestMap= new LinkedMultiValueMap<String, String>();
requestMap.add("client_id", "123");
requestMap.add("app_code", "abc");
HttpEntity requestEntity = new HttpEntity(requestMap,null);
String responseAsString = restTemplate.postForObject("http://localhost:8088/rest/post",requestEntity,String.class);
System.out.println("post1:" + responseAsString);
//发送json数据
requestMap= new LinkedMultiValueMap<String, String>();
requestMap.add("client_id", "123");
requestMap.add("app_code", "abc");
// 必须加上header说明
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> jsonEntity = new HttpEntity<String>(new Gson().toJson(requestMap),headers);
responseAsString = restTemplate.postForObject("http://localhost:8088/rest/post2",jsonEntity,String.class);
System.out.println("postJson:" + responseAsString);
}
exchange
/**
* 可以指定调用类型
*/
public static void exchange(){
RestTemplate restTemplate = new RestTemplate();
//发送post
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
params.add("pageNo",1);
HttpEntity entity = new HttpEntity(params,null);
Notice testList = restTemplate.exchange("http://localhost:8088/rest/exchange1", HttpMethod.POST, entity, new ParameterizedTypeReference<Notice>() {}).getBody();
System.out.println(testList.toString());
Notice notice = restTemplate.exchange("http://localhost:8088/rest/exchange2?pageNo=66", HttpMethod.GET, null,new ParameterizedTypeReference<Notice>() {}).getBody() ;
System.out.println(notice.toString());
}
对应controller
@RestController
@RequestMapping("/rest")
public class RestController {
@GetMapping("/notice")
public Notice notice(){
Notice notice = new Notice();
notice.setMsg("666");
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@GetMapping("/hello/5")
public Notice hello5(){
Notice notice = new Notice();
notice.setMsg("hello5");
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@GetMapping("/page")
public Notice page(HttpServletRequest request){
String start = request.getParameter("start");
String page = request.getParameter("page");
Notice notice = new Notice();
notice.setMsg("start:" + start + ", page:" + page);
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@PostMapping("/post")
public Notice post1(HttpServletRequest request){
String client_id = request.getParameter("client_id");
String app_code = request.getParameter("app_code");
Notice notice = new Notice();
notice.setMsg("client_id:" + client_id + ", app_code:" + app_code);
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@PostMapping("/post2")
public Notice post2(@RequestBody String json){
Notice notice = new Notice();
notice.setMsg("json:" + json );
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@PostMapping("/exchange1")
public Notice exchange1(HttpServletRequest request){
String pageNo = request.getParameter("pageNo");
Notice notice = new Notice();
notice.setMsg("post---pageNo:" + pageNo);
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
@GetMapping("/exchange2")
public Notice exchange2(HttpServletRequest request){
String pageNo = request.getParameter("pageNo");
Notice notice = new Notice();
notice.setMsg("get---pageNo:" + pageNo);
notice.setStatus(1);
notice.setData(new LinkedList<>());
return notice;
}
}
还没有评论,来说两句吧...