Java - 在WebService中使用Client调用三方的RestAPI

简介:

选型

其实在自己的项目里面也有类似的调用,当时使用的是“JAXRSClientFactory”获得静态代理client。 由于这种方式需要依赖于远程调用的webservice接口(需要引入别人的jar包)。这就造成了高耦合。因此不适用。

所以需要以一种低耦合的方式来实现。便有了选型的想法。

 

在网上搜索一番后,基本定型为两种方式:

1.HttpClient

2.RestTemplate

 

接下来就分别列出两种方式的实现代码

 

HttpClient

import java.io.IOException;  
import java.text.MessageFormat;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.concurrent.TimeUnit;  
  
import org.apache.http.NameValuePair;  
import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.ResponseHandler;  
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.impl.client.BasicResponseHandler;  
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.message.BasicNameValuePair;  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  

public class HttpClientUtil {  

    private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class);  
    private CloseableHttpClient httpClient = HttpClients.createDefault();    
    public static String executePost(String url, String tdfBase64) throws Exception {  
      
            String result = null;  
            HttpPost httpPost = new HttpPost(url);  
            
            httpPost.setEntity(new HttpEntity<String>(tdfBase64));
      
            HttpResponse response = httpClient.execute(httpPost);  
            if (response != null) {  
                HttpEntity resEntity = response.getEntity();  
                if (resEntity != null) {  
                    result = EntityUtils.toString(resEntity, "utf-8");  
                }  
            }  
      
            return result;  
        }  
    }    
    public static void main(String[] args) {        // TODO Auto-generated method stub
        String url = "http://169.8.160.201:8080/xx/Webservice/Submit";  
        String base64Tdf = "MS4wMToxMzIdMS4wMjowMjAxHTEuMDM6MR8wMR4yHzAwHTEuMDQ6SVJRHTEuMDU6MjAxNjA1MDQdMS4wNjoxHTEuMDc6Q09HRU5UHTEuMDg6VEhBSUxBTkQdMS4wOTpTRVFVRU5DRU5PMTIzNB0xLjExOjE5LjY5HTEuMTI6MTkuNjkcMi4wMDE6MzEdMi4wMDI6MDAdMi4xNzY6MDA3MDA5HA==";
        HttpClientUtil client = new HttpClientUtil();
        String result=client.executePost(url, base64Tdf, "");
        System.out.println(result);
    }

 

 

RestTemplate

package com.biolive.client;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.HttpEntity;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestClientException;import org.springframework.web.client.RestTemplate;public class RestTemplateClient {    
    
    private static final Logger log = LoggerFactory.getLogger(RestTemplateClient.class);    private static final int connectTimeout= 5000;    private static final int readTimeOut=5000;    private RestTemplate restTemplate;    
    public RestTemplateClient(){
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(connectTimeout);
        requestFactory.setReadTimeout(readTimeOut);
        
        restTemplate = new RestTemplate(requestFactory);
    }    
    public String executePost(String url, String base64Tdf){
        String result = null;        
       
        HttpEntity<String>request = new HttpEntity<String>(base64Tdf);        try{
            result=restTemplate.postForObject(url, request, String.class);
        }catch(RestClientException ex){
            ex.printStackTrace();  
            log.info("call post interface error: " + ex.getMessage());  
        }    
        
        return result;
    }    public static void main(String[] args) {        // TODO Auto-generated method stub
        String url = "http://169.8.160.201:8080/xx/Webservice/Submit";  
        String base64Tdf = "MS4wMToxMzIdMS4wMjowMjAxHTEuMDM6MR8wMR4yHzAwHTEuMDQ6SVJRHTEuMDU6MjAxNjA1MDQdMS4wNjoxHTEuMDc6Q09HRU5UHTEuMDg6VEhBSUxBTkQdMS4wOTpTRVFVRU5DRU5PMTIzNB0xLjExOjE5LjY5HTEuMTI6MTkuNjkcMi4wMDE6MzEdMi4wMDI6MDAdMi4xNzY6MDA3MDA5HA==";
        RestTemplateClient client = new RestTemplateClient();
        String result=client.executePost(url, base64Tdf);
        System.out.println(result);
    }
    
    
}


本文转自 bxst 51CTO博客,原文链接:http://blog.51cto.com/13013670/1946994

相关文章
|
9月前
|
存储 分布式计算 安全
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)(一)
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)
345 0
|
Java
java使用Quartz任务调用crontab表达式的时候报错:Based on configured schedule, the given trigger will never fire
java使用Quartz任务调用crontab表达式的时候报错:Based on configured schedule, the given trigger will never fire
396 0
java使用Quartz任务调用crontab表达式的时候报错:Based on configured schedule, the given trigger will never fire
|
6月前
|
Java 微服务
【Java异常】com.netflix.client.ClientException: Load balancer does not have available server for client
【Java异常】com.netflix.client.ClientException: Load balancer does not have available server for client
119 0
|
4月前
|
SQL Java 数据库连接
Could not open client transport with JDBC Uri: jdbc:hive2://192.168.88.10:10000: java.net.ConnectExc
Could not open client transport with JDBC Uri: jdbc:hive2://192.168.88.10:10000: java.net.ConnectExc
96 0
|
4月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
57 0
|
7月前
|
Java API 数据安全/隐私保护
Elasticsearch Java API Client 开发
本场景主要介绍如何使用 Elasticsearch Java API Client 进行开发,实现常用的 CRUD 操作。
146 0
|
8月前
|
IDE Java 应用服务中间件
Java WebService如何生成PDF文件
在Web应用开发中,生成PDF文件是一项非常常见的需求。本文将介绍如何使用Java WebService来生成PDF文件。
79 0
java对接webservice服务实现推送
前不久接到一个任务需要将我们平台的内容推送到第三方的一个webService服务中,我们平台接口使用java来做的,所以需要通过java调用webService服务实现推送效果,不多说直接上干货。
|
9月前
|
Java 存储 Linux
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)(三)
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)
111 0
|
9月前
|
存储 算法 网络协议
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)(二)
分布式文件系统介绍与minio介绍与使用(附minio java client 使用)
494 0