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<String> requestEntity = new HttpEntity<String>(null, headers);
    //3,得到相应体
    ResponseEntity<IdentityInfoRespDto> response = restTemplate.exchange(
       url, HttpMethod.GET, requestEntity, IdentityInfoRespDto.class);    
    IdentityInfoRespDto body = response.getBody();

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

String url = perfectOldSystemConfig.getExpress() + expressNo;
ResponseEntity<ExpressListRespDto> 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<String, String> map = new LinkedMultiValueMap<String, String>(); 
 map.add("storeCode", repairInfoReqDto.getStoreCode()); 
 map.add("memberNO", repairInfoReqDto.getMemberNO()); 

 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map,       headers); 

 ResponseEntity<RepairInfoDataRespDto> 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplate是Spring Framework提供的一个用于访问Rest服务的客户端工具,它封装了一些常用的HTTP请求操作,比如GET、POST等。使用RestTemplate可以方便地发送HTTP请求,并能够自动将响应转换成指定的对象类型。下面是RestTemplate的使用方法: 1. 创建RestTemplate对象 RestTemplate对象可以通过直接实例化或者通过注入的方式来创建。直接实例化方式如下: ``` RestTemplate restTemplate = new RestTemplate(); ``` 2. 发送HTTP请求 RestTemplate支持多种HTTP请求方法,比如GET、POST、PUT、DELETE等。以下是发送GET请求的示例代码: ``` String url = "http://example.com/api/users/{userId}"; Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("userId", "123"); User user = restTemplate.getForObject(url, User.class, uriVariables); ``` 其中,url为请求的URL,uriVariables为请求参数,User.class为响应结果需要转换的类型。其他HTTP请求方法也类似,只需要替换对应的方法名即可。 3. 处理响应结果 RestTemplate支持多种方式处理响应结果,比如将响应结果转换成Java对象、将响应结果转换成JSON字符串等。以下是将响应结果转换成Java对象的示例代码: ``` String url = "http://example.com/api/users/{userId}"; Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("userId", "123"); User user = restTemplate.getForObject(url, User.class, uriVariables); ``` 其中,url为请求的URL,uriVariables为请求参数,User.class为响应结果需要转换的类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值