RestTemplate 常用的方法

exchange() :在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的(包含报文头)多了requestEntity

1,获取Url  2,获取请求报文头  3,得到响应体

public Boolean idcardVerification(String idCard, String name) {
    //1,获取Url
    String url = SystemConfig.getIdVerifyUrl() + "?idCard=" + idCard + "&name=" + name;
    String appcode = SystemConfig.getAppcode();
    //2,获取请求报文头
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "APPCODE " + appcode);
    HttpEntity requestEntity = new HttpEntity(null, headers);
    //3,得到相应体
    ResponseEntity response = restTemplate.exchange(
       url, HttpMethod.GET, requestEntity, IdentityInfoRespDto.class);    
    IdentityInfoRespDto body = response.getBody();

getForEntity()  :发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象

String url = perfectOldSystemConfig.getExpress() + expressNo;
ResponseEntity responseEntity = restTemplate.getForEntity(url, ExpressListRespDto.class);
ExpressListRespDto 是你要映射的Dto,(包含code,msg,data)

postForEntity()  :POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的,多了requestEntity

HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

 MultiValueMap map = new LinkedMultiValueMap(); 
 map.add("storeCode", repairInfoReqDto.getStoreCode()); 
 map.add("memberNO", repairInfoReqDto.getMemberNO()); 

 HttpEntity> requestEntity = new HttpEntity>(map,       headers); 

 ResponseEntity responseEntity = restTemplate.postForEntity( 
      url, requestEntity, RepairInfoDataRespDto.class); 
 RepairInfoDataRespDto body=responseEntity.getBody(); 

阿里--身份证验证
地址:https://market.aliyun.com/products/57000002/cmapi022049.html?spm=5176.10695662.1996646101.searchclickresult.17154decmm4bsJ#sku=yuncode1604900000

你可能感兴趣的:(RestTemplate 常用的方法)