RestTemplate和URLConnection 访问外部第三方接口所实现的方法的记录

需要访问其他外部接口的方法,通过网上的借鉴,记录一下,方便后续借鉴

RestTemplate POST 方法

 		try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            List<String> cookies =new ArrayList<String>();
            // 设置Cookie 
            cookies.add("key"="value; Path=/; HttpOnly");   
                		       		  
            headers.put(HttpHeaders.COOKIE,cookies);       
			//设置类型
            headers.setContentType(MediaType.APPLICATION_JSON);
//           想要设置其他属性 用headers.set(key,value) 比如token或者密钥

            MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
            //设置body参数
            map.add("FileID", "123");
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
            //设置url及方法头和方法体进行连接
            ResponseEntity<String> response = restTemplate.postForEntity("http://127.0.0.1:8080/**",request,String.class);
            //获取返回值
           body = response.getBody();
            
        } catch (RestClientException e) {
           //异常处理
        }

RestTemplate GET方法

不带请求头的可以用以下方法实现

//第一种方式 getForObject
   try {
            RestTemplate restTemplate = new RestTemplate();
            String url = "http://127.0.0.1:8080/**";
			//第一个参数为访问url,第二个为返回参数 这里返回的String 所以可以直接获取
            String result = restTemplate.getForObject(url,String.class);
            
        }catch (Exception e){
		//异常处理
        }
//第二种方式 getForEntity
   try {
            RestTemplate restTemplate = new RestTemplate();
            String url = "http://127.0.0.1:8080/**";
			//第一个参数为访问url,第二个为返回参数 这里返回的Entity实体类
           ResponseEntity<Entity> resEntity=restTemplate.getForEntity(url,Entity.class);
           //需要通过 getBody()获取
           resEntity.getBody();

        }catch (Exception e){
		//异常处理
        }

带请求头的用以下方法实现

		try {
   			RestTemplate restTemplate = new RestTemplate();
   			//创建请求头
            HttpHeaders headers = new HttpHeaders();
            String url = "http://127.0.0.1:8080/**";
            //设置请求头
           List<String> cookies =new ArrayList<String>();
            // 设置Cookie 
            cookies.add("key"="value; Path=/; HttpOnly");   	       		  
            headers.put(HttpHeaders.COOKIE,cookies);       
			//设置类型
            headers.setContentType(MediaType.APPLICATION_JSON);
			//想要设置其他属性 用headers.set(key,value) 比如token或者密钥
	      
            HttpEntity<String> returnData= restTemplate.exchange(url, HttpMethod.GET,new HttpEntity<>(headers),String.class);
            
             returnData.getBody();
    	}catch (Exception e){
		//异常处理
        }

URLConnection 方法

链接参考如下: URLConnection 方法.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值