java调用公共数据接口: webxml
前言
java日常开发工作中最常见的就是:调接口!、调接口!!、调接口!!! 对于老鸟来说,分分钟把数据给你拿过来。但是对新手来说,可能会比较懵圈,想要学习这块内容又没有路径,博主找了一些公共的数据接口给大家作为参考,博主以拿到数据为目标,有更好的方法,欢迎大家评论!- 本文调用的是webxml上的接口,后续在项目中会逐步添加以下数据接口:
- 上海市公共数据开放平台
- 阿里云云市场接口(免费+收费)
- 苏州市公共数据平台
- 等等…
为方便以后添加新内容,文章中的步骤比较繁琐(其实这个功能也就3行代码),比较着急的同学直接去impl看!
一、以webxml上的一个接口为案例
- 我们访问: http://www.webxml.com.cn可以看到有很多公共接口,我们选一个简单的接口作为案例:腾讯QQ在线状态 WEB 服务
二、查看接口文档
不管会不会,确认目标后第一步:看文档!我们点开
http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx:
可以看到,这个服务比较基础,只提供qqCheckOnline这一个功能,继续点进去。
(ps:工作中对接的接口经常会看到一堆方法,一个功能往往会提供很多种姿势去调用。。。)
1.查看支持的调用方式
红色框框内表示该接口支持的调用协议,一般开放的就是SOAP接口和HTTP接口(关于两者的区别,请点击这里),本文使用调用文档中的http接口,如果发现只支持soap接口,请看这篇:soap接口调用案例(完成中)
2.确认接口地址与参数
从说明中可以直接找到:
host: ws.webxml.com.cn
接口地址: /webservices/qqOnlineWebService.asmx/qqCheckOnline
参数:qqCode (String类型)
返回值:String
三、编码(找代码直接跳这里!)
确认好接口内容,新建一个springboot项目,万事俱备,开始敲代码!
1.配置RestTemplate
在config包里面新建ApplicationConfig类,配一个RestTemplate。
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author: pzc
* @Date: 2020-12
* @Description: *
*/
@Configuration
//todo --------------------------------------------------basePackages 改成自己的路径
@ComponentScan(basePackages = "com.example.demo")
public class ApplicationConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.编写service和serviceImpl(按正规写法来)
- WebxmlService
package com.example.demo.service;
public interface WebxmlService {
/*
获得腾讯QQ在线状态
输入参数:QQ号码 String,默认QQ号码:8698053。\
返回数据:String,Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量
*/
String qqCheckOnline(String qqCode);
}
- WebxmlServiceImpl(想直接用的可以只看impl)
package com.example.demo.service.impl;
import com.example.demo.service.WebxmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
@Service
public class WebxmlServiceimpl implements WebxmlService {
@Autowired
private RestTemplate restTemplate;
String host = "http://ws.webxml.com.cn";
@Override
public String qqCheckOnline(String qqCode) {
String apiUrl = "/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode="+qqCode;
HashMap<String, String> params = new HashMap<>();
params.put("qqCode", qqCode);
String result = restTemplate.getForObject(host + apiUrl, String.class);
return result;
}
}
3.写个单元测试
package com.example.demo;
import com.example.demo.service.WebxmlService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WebXmlTests {
@Autowired
private WebxmlService webxmlService;
@Test
void contextLoads() {
String result = webxmlService.qqCheckOnline("8698053");
System.err.println(result);
}
}
哦豁!是个xml,先来个XMLUtils直接复制压压惊!
4.再来个单元测试:
package com.example.demo;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.eunm.QQOnlineStatusEnum;
import com.example.demo.service.WebxmlService;
import com.example.demo.utils.XMLUtils;
import org.jdom2.JDOMException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class WebXmlTests {
@Autowired
private WebxmlService webxmlService;
@Test
void contextLoads() throws JDOMException, IOException {
String result = webxmlService.qqCheckOnline("8698053");
JSONObject resultJsonObject = XMLUtils.XMLReader(result,"utf-8");
String resultJsonObjectStr = resultJsonObject.get("string").toString();
QQOnlineStatusEnum qqOnlineStatusEnum = QQOnlineStatusEnum.parseKey(resultJsonObjectStr);
System.err.println(qqOnlineStatusEnum.getValue());
}
}
哦豁,搞定了!这里用了枚举,也贴给大家:
package com.example.demo.eunm;
/**
* @Author: pzc
* @Date: 2020-12
* @Description: *
*/
public enum QQOnlineStatusEnum {
OFFLINE("N", "离线"),
OUTNUNMBER_ERROR("V", "免费用户超过数量"),
QQCODE_ERROR("E", "QQ号码错误"),
SUCCESS("Y", "在线"),
USER_ERROR("A", "商业用户验证失败"),
NONE("X", "未知问题,请联系管理员!"),
;
private String key;
private String value;
QQOnlineStatusEnum(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public static QQOnlineStatusEnum parseValue(String value) {
for (QQOnlineStatusEnum item : QQOnlineStatusEnum.values()) {
if (value.equals(item.getValue())) {
return item;
}
}
return NONE;
}
public static QQOnlineStatusEnum parseKey(String key) {
for (QQOnlineStatusEnum item : QQOnlineStatusEnum.values()) {
if (key.equals(item.getKey())) {
return item;
}
}
return NONE;
}
}
6.项目结构也贴一下(顺带秀一下idea背景- -/):
四、总结
目前完成了项目的第一步,后续有空会添加更多的新的接口功能。
下一篇:第二篇:java调用公共数据接口:上海市车辆基本信息查询(java调用soap接口)
版权声明:本文为weixin_38680917原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。