如何调用封装工具类调用网上接口查询工作日
程序员文章站
2022-05-16 21:25:28
...
如何调用封装工具类调用网上接口查询工作日
这里的编辑器是STS,用的springboot集成环境;
先引进pom.xml依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>boloDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>boloDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<!-- <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId>
<version>1.6.2</version> </dependency> -->
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<!-- <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId>
<version>1.1.1</version> </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>3.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
public class OkTool {
@Autowired
//private OkHttpClient.Builder cb;
private OkHttpClient client;
@Autowired
private Request.Builder rb;
private Map<String, String>formData=new HashMap<>();
private String url;
public OkTool url(String url) {
this.url=url;
return this;
}
public OkTool addFormData(String key,String value) {
formData.put(key, value);
return this;
}
public String get() throws IOException {
Response response=null;
if (formData.size()==0) {
response=client.newCall(rb.url(url).build()).execute();
}else {
StringBuffer sb=new StringBuffer();
sb.append(url);
if (!url.contains("?")) {
sb.append("?");
}
else {
sb.append("&");
}
Set<String>key=formData.keySet();
for (String string : key) {
sb.append(string+"="+formData.get(string)+"&");
}
url=sb.toString().substring(0,sb.length()-1);
response=client.newCall(rb.url(url).build()).execute();
}
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
public String post() throws IOException {
Response response=null;
FormBody.Builder fb=new FormBody.Builder();
if (formData.size()==0) {
response=client.newCall(rb.url(url).build()).execute();
}else {
Set<String>key=formData.keySet();
for (String string : key) {
fb.add(string, formData.get(string));
}
response=client.newCall(rb.url(url).post(fb.build()).build()).execute();
}
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
public String json() throws IOException {
ObjectMapper ob=new ObjectMapper();
Response response=null;
//没有加入post的数据,所以自动的用get方法
if (formData.size()==0) {
response=client.newCall(rb.url(url).build()).execute();
}else {
MediaType mt=MediaType.parse("application/json; charset=utf-8");
//创建以json方式提交的body
RequestBody body=RequestBody.create(mt, ob.writeValueAsString(formData));
response=client.newCall(rb.url(url).post(body).build()).execute();
}
//清除数据
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
public String put() throws IOException {
Response response=null;
FormBody.Builder fb=new FormBody.Builder();
if (formData.size()==0) {
response=client.newCall(rb.url(url).build()).execute();
}else {
Set<String>key=formData.keySet();
for (String string : key) {
fb.add(string, formData.get(string));
}
response=client.newCall(rb.url(url).put(fb.build()).build()).execute();
}
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
public String delete() throws IOException {
Response response=null;
FormBody.Builder fb=new FormBody.Builder();
if (formData.size()==0) {
response=client.newCall(rb.url(url).delete().build()).execute();
}else {
Set<String>key=formData.keySet();
for (String string : key) {
fb.add(string, formData.get(string));
}
response=client.newCall(rb.url(url).delete(fb.build()).build()).execute();
}
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
public String patch() throws IOException {
Response response=null;
FormBody.Builder fb=new FormBody.Builder();
if (formData.size()==0) {
response=client.newCall(rb.url(url).build()).execute();
}else {
Set<String>key=formData.keySet();
for (String string : key) {
fb.add(string, formData.get(string));
}
response=client.newCall(rb.url(url).patch(fb.build()).build()).execute();
}
url=null;
formData.clear();
if (response!=null) {
return response.body().string();
}
return null;
}
}
@Configuration
public class BeanConfig {
@Bean
public OkHttpClient.Builder clientBuilder() {
OkHttpClient.Builder builder=new OkHttpClient.Builder();
return builder;
}
@Bean
public Request.Builder requestBuilder() {
Request.Builder builder=new Request.Builder();
builder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
return builder;
}
@Bean
public OkHttpClient okHttpClient(@Autowired OkHttpClient.Builder builder) {
return builder.build();
}
@Bean
public OkTool okTool() {
return new OkTool();
}
}
@Component
public class HolidayQuery {
@Autowired
private OkTool ok;
private String apiAddress="接口地址";
//成功则返回一个 日期:类型的值 0代表
/**
* <p>方法名称: queryByApi</p>
* <p>描述:查询节假日的api,可以传入一个date值</p>
* @param date String类型 格式为yyyyMMdd 例如20180921
* @return map 一个map key为日期 value 为0 工作日 1为周六日 2为节假日
* @throws IOException Map<String,String> 返回类型
*/
public Map<String, String> queryByApi(String date) throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
String str=ok.url(apiAddress).addFormData("date", date).get();
HashMap<String, String> map=objectMapper.readValue(str, HashMap.class);
if (String.valueOf(map.get("code")).equals("10000")) {
String temp=String.valueOf(map.get("data"));
map.clear();
map.put(date, temp);
return map;
}
return objectMapper.readValue(str, HashMap.class);
}
/**
* <p>方法名称: queryByApi</p>
* <p>描述:TODO(这里用一句话描述这个方法的作用)</p>
* @param dates 任意长度的数组 格式为yyyyMMdd 例如20180921
* @return map 一个map key为日期 value 为0 工作日 1为周六日 2为节假日
* @throws IOException Map<String,String> 返回类型
*/
public Map<String, String> queryByApi(String... dates) throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
Map<String, String>resmap=new HashMap<>();
for (String date : dates) {
String jsonstr=ok.url(apiAddress).addFormData("date", date).get();
HashMap<String, String> map=objectMapper.readValue(jsonstr, HashMap.class);
if (String.valueOf(map.get("code")).equals("10000")) {
String temp=String.valueOf(map.get("data"));
resmap.put(date, temp);
}
}
return resmap;
}
}
后面自己去写个测试就可以了,不会的可以留言;
推荐阅读