前言
這次也是頭一次接觸對(duì)接第三方WebService接口,這技術(shù)都很老了,使用postman測(cè)試的時(shí)候還找了半天資料??。
關(guān)于postman測(cè)試
一般來說第三方都會(huì)限制ip這些,需要注意的是,給到的接口地址是能用公網(wǎng)進(jìn)行訪問的哦。
1、拿到接口路徑
http://111.111.11.1:111/services/infoWebService?wsdl
這個(gè)當(dāng)然是不可以訪問的,是假的??。
2、瀏覽器鍵入地址
出現(xiàn)這樣的xml就代表接口沒毛病哦
3、來到postman 還是同樣的操作,將第三方接口地址設(shè)為POST請(qǐng)求,如果請(qǐng)求不到可以去掉路徑上的 ?wsdl,設(shè)置請(qǐng)求頭 Content-Type:text/xml;charset=urf-8。
注意選中 raw 格式選擇 XML 。
標(biāo)號(hào) 1 的地方就是我們的方法名稱。
標(biāo)號(hào) 2 的地方就是我們的 targetNamespace 里的內(nèi)容。
標(biāo)號(hào) 3 的地方就是我們的方法返回類型。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ns1:applyInfo xmlns:ns1="http://service.webservice.catalog.infotrust.com">
<ns1:in0>
<![CDATA[
{
"token":"31cf2fba490b4714aa0b601fe0e25481",
"rescode":"892d615e9cec4b0a8fff9da2099bf2e7"
}
]]>
</ns1:in0>
</ns1:applyInfo>
</soap:Body>
</soap:Envelope>
參數(shù)就需要你自己去參照第三方給到你的文檔了哦。
4、返回?cái)?shù)據(jù)
有沒有發(fā)現(xiàn)什么不一樣地方呢,沒錯(cuò)返回的也是xml格式的哦。
還可以發(fā)現(xiàn)數(shù)據(jù)都在我們的data里面,是JSON格式。
這里需要注意的是,想要拿到JSON對(duì)象,需要對(duì)返回的數(shù)據(jù)進(jìn)行截取,詳情看后臺(tái)。
后臺(tái)調(diào)用WebService第三方接口
-
根據(jù)第三方提供的接口文檔,創(chuàng)建對(duì)應(yīng)的實(shí)體類,字段最好一致,或者再多寫個(gè)映射可以實(shí)現(xiàn)本地?cái)?shù)據(jù)庫(kù)字段與第三方數(shù)據(jù)庫(kù)字段不一致 ! !
-
想要調(diào)用接口,還需要登錄拿到Token,使用Token 和資源碼調(diào)用接口,人家給的文檔已經(jīng)明確說明了。
在postman中:
可以看到是需要(用戶名+賬號(hào)+時(shí)間戳)的MD5值,那還需要一個(gè)操作獲取時(shí)間戳和MD5Key
/**
* 獲取MD5Key和時(shí)間戳
*/
@Slf4j
public class DSFToken {
/**
* 獲取MD5Key和時(shí)間戳
*/
public Map<String,String> getMd5KeyAndTimeStamp()throws Exception{
Map<String,String> map = new HashMap<>();
Long timestamp = getTimestamp();
String md5key = getMD5s("test123tD8#mE5["+timestamp);
map.put("timestamp",timestamp.toString());
map.put("md5key",md5key);
return map;
}
/**
* 獲取當(dāng)前時(shí)間戳
*/
public Long getTimestamp() throws Exception {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Instant instant = timestamp.toInstant();
Timestamp tsFromInstant = Timestamp.from(instant);
return tsFromInstant.getTime();
}
/**
* 獲取MD5值
* @param plainText
* @return
*/
public String getMD5s(String plainText)throws Exception {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
//32位加密
return buf.toString();
// 16位的加密
//return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
- 這個(gè)時(shí)候我們可以正常獲取token了
/**
* 獲取登錄token
* @return
* @throws Exception
*/
public String getToken() throws Exception {
DSFToken dsf = new DSFToken();
Map<String,String> map = dsf.getMd5KeyAndTimeStamp();
//獲取時(shí)間戳
String timestamp = map.get("timestamp");
//獲取md5key值
String md5key = map.get("md5key");
//WebService外網(wǎng)訪問ip地址
String SERVER_IP_TEST = "http://********/services/infoWebService";
//通過StringBuilder拼接請(qǐng)求
StringBuilder paramValue = new StringBuilder();
paramValue.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
"<soap:Body>\n" +
"<ns1:getSafeToken xmlns:ns1=\"http://service.webservice.catalog.infotrust.com\">\n" +
"<ns1:in0>\n" +
"<![CDATA[\n" +
"{\"username\":\"test123\",\"md5key\":\""+ md5key +"\",\"timestamp\":\""+timestamp+"\"}\n" +
"]]>\n" +
"</ns1:in0>\n" +
"</ns1:getSafeToken>\n" +
"</soap:Body>\n" +
"</soap:Envelope>");
//調(diào)用post請(qǐng)求方法
String result = sendPost(SERVER_IP_TEST,paramValue.toString());
//由于返回的是XML我們需要對(duì)返回的數(shù)據(jù)進(jìn)行截取,才能獲得想要的
String str1 = result.substring(result.indexOf("t>"),result.indexOf("</n")).substring("t>".length());
if (str1 != null) {
//轉(zhuǎn)JSONObject格式
JSONObject jsonObject = JSON.parseObject(str1);
if(jsonObject.get("token") != null){
// redisDao.setStringAndTime("dsfToken",jsonObject.getString("token"),3600*24);//redis緩存一天
return jsonObject.getString("token");
}
}
throw new BaseException("聯(lián)系管理員");
}
4. 拿到Token調(diào)用接口,使用Token 和資源碼 (固定的) 獲取數(shù)據(jù) 并將數(shù)據(jù)存儲(chǔ)到本地?cái)?shù)據(jù)庫(kù)
public List<AeoInfo> updateAeoInfo() throws Exception {
//資源碼
String rescode = "c9769a4d208d41a99c1d847b68ccf1b4";
//獲取token
String token = getToken();
// if(token==null){
// redisDao.setStringAndTime("dsfToken",getToken(),3600*24);//redis緩存一天
// token = redisDao.get("dsfToken");
// }
//WebService外網(wǎng)訪問ip地址
String SERVER_IP_TEST = "http://********/services/infoWebService";
//通過StringBuilder拼接請(qǐng)求
StringBuilder paramValue = new StringBuilder();
paramValue.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
"<soap:Body>\n" +
"<ns1:applyInfo xmlns:ns1=\"http://service.webservice.catalog.infotrust.com\">\n" +
"<ns1:in0>\n" +
"<![CDATA[\n" +
"{\n" +
"\"token\":\"" + token +"\",\n" +
"\"rescode\":\"" + rescode + "\"}\n" +
"]]>\n" +
"</ns1:in0>\n" +
"</ns1:applyInfo>\n" +
"</soap:Body>\n" +
"</soap:Envelope>");
//調(diào)用post請(qǐng)求方法
String result = sendPost(SERVER_IP_TEST,paramValue.toString());
//截取字符串
String str1 = result.substring(result.indexOf("t>"),result.indexOf("</n")).substring("t>".length());
if (str1 != null) {
//轉(zhuǎn)JSONObject格式
JSONObject jsonObject = JSON.parseObject(str1);
if (jsonObject.get("data") != null) {
//轉(zhuǎn)對(duì)象格式
List<AeoInfo> entisyList = JSON.parseArray(jsonObject.getString("data"),AeoInfo.class);
if(entisyList != null && entisyList.size()>0){
for (AeoInfo entity : entisyList) {
//通過某個(gè)字段可以實(shí)現(xiàn)數(shù)據(jù)庫(kù)中沒有就新增,有就更新的效果,JPA的寫法
//你們對(duì)接接口的時(shí)候,需要自己去選擇以哪個(gè)字段為準(zhǔn)哦
AeoInfo adminLicensing = aeoInfoRepository.findByRN(entity.getRN());
if(adminLicensing==null){
//保存
aeoInfoRepository.save(entity);
}
}
}
return entisyList;
}
}
return null;
}
工具
用來發(fā)送Post請(qǐng)求的方法,用到的是restTemplate,你也可以換別的哦文章來源:http://www.zghlxwxcb.cn/news/detail-410810.html
@Autowired
private RestTemplate restTemplate;
/**
* post請(qǐng)求、請(qǐng)求參數(shù)為json
* @return
*/
public String sendPost(String uri, String params) {
ResponseEntity<String> apiResponse = restTemplate.postForEntity
(
uri,
generatePostJson(params),
String.class
);
return apiResponse.getBody();
}
/**
* 生成post請(qǐng)求的JSON請(qǐng)求參數(shù)
* @return
*/
public HttpEntity<String> generatePostJson(String params) {
//如果需要其它的請(qǐng)求頭信息、都可以在這里追加
HttpHeaders httpHeaders = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
httpHeaders.setContentType(type);
HttpEntity<String> httpEntity = new HttpEntity<>(params, httpHeaders);
return httpEntity;
}
XML配置
<!--定時(shí)任務(wù)和@Slf4j注解日志的依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<!--json依賴-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<!-- apache md5 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- RestTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
至此大公告成!文章來源地址http://www.zghlxwxcb.cn/news/detail-410810.html
到了這里,關(guān)于對(duì)接 Web Service第三方接口的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!