前提
实际上开发一个Java-SDK的过程,实际上也就是开发一个基本java项目并打包成jar包的过程,然后可以被其它项目引入使用。
开发Java-SDK
本例介绍开发一个向某一数据接口发送请求并返回结果的SDK
1、新建一个Maven工程test-sdk-java
2、编辑pom文件,引入需要的jar包,若不需要第三方jar包也可以不引入
本例使用了hutool工具包
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 4.0.0
6
7 org.example
8 test-sdk-java
9 1.0-SNAPSHOT
10
11
12
13 cn.hutool
14 hutool-all
15 5.3.7
16
17
18
19
3、编辑封装类,可以封装一些必要的逻辑
MyHttpRequest.java
1 packagecom.test.api.bean;2
3 importcn.hutool.http.HttpRequest;4 importcn.hutool.http.HttpResponse;5
6 public classMyHttpRequest {7
8 privateHttpRequest httpRequest;9
10 publicMyHttpRequest(String url){11 httpRequest =HttpRequest.get(url);12 //可以封装其他逻辑
13 }14
15 publicMyHttpResponse execute(){16 if(httpRequest == null) {17 throw new NullPointerException("httpRequest is empty !");18 }19 HttpResponse httpResponse =httpRequest.execute();20 MyHttpResponse myHttpResponse = newMyHttpResponse();21 myHttpResponse.setHttpResponse(httpResponse);22 returnmyHttpResponse;23 }24
25 public static voidmain(String[] args) {26 MyHttpRequest myHttpRequest = new MyHttpRequest("https://www.sina.com");27 MyHttpResponse myHttpResponse =myHttpRequest.execute();28 int status =myHttpResponse.getStatus();29 if(status == 200) {30 System.out.println(myHttpResponse.body());31 }else{32 System.out.println("status === " +status );33 System.out.println("请求失败!");34 }35 }36 }
MyHttpResponse.java
1 packagecom.test.api.bean;2
3 importcn.hutool.http.HttpResponse;4
5 public classMyHttpResponse{6
7 HttpResponse httpResponse;8
9 publicHttpResponse getHttpResponse() {10 returnhttpResponse;11 }12
13 public voidsetHttpResponse(HttpResponse httpResponse) {14 this.httpResponse =httpResponse;15
16 }17
18 public intgetStatus() {19 if(httpResponse == null) {20 throw new NullPointerException("httpResponse is empty !");21 }22 returnhttpResponse.getStatus();23 }24
25 publicString body(){26 if(httpResponse == null) {27 throw new NullPointerException("httpResponse is empty !");28 }29 returnhttpResponse.body();30 }31 }
4、编辑一个客户端MyHttpClient.java
1 packagecom.test.api;2
3 importcom.test.api.bean.MyHttpRequest;4 importcom.test.api.bean.MyHttpResponse;5
6 public classMyHttpClient {7
8 private staticMyHttpClient myHttpClient;9
10 /**
11 *@returnInstance12 */
13 public staticMyHttpClient getInstance() {14 synchronized (MyHttpClient.class) {15 if(myHttpClient == null) {16 myHttpClient = newMyHttpClient();17 }18 }19 returnmyHttpClient;20 }21
22 publicMyHttpResponse execute(String url){23 MyHttpRequest myHttpRequest = newMyHttpRequest(url);24 returnmyHttpRequest.execute();25 }26
27 }
5、打包
1)调用方是maven工程,调用时直接使用依赖引入的方法使用sdk
使用mvn命令:mvn clean install打包安装
2)调用方是非maven工程,调用时直使用 sdk jar包
使用Java-SDK
1、新建一个Maven工程test-sdk-java-test
2、引入sdk依赖
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 4.0.0
6
7 org.example
8 test-sdk-java-test
9 1.0-SNAPSHOT
10
11
12
13 org.example
14 test-sdk-java
15 1.0-SNAPSHOT
16
17
18
3、编写主类进行测试
1 public classMyMain {2 public static voidmain(String[] args) {3 //配置
4 String url = "https://www.sina.com";5 //获取SDK中客户端
6 MyHttpClient myHttpClient =MyHttpClient.getInstance();7 //执行业务
8 MyHttpResponse myHttpResponse =myHttpClient.execute(url);9 int status =myHttpResponse.getStatus();10 if(status == 200) {11 System.out.println("请求成功");12 System.out.println(myHttpResponse.body());13 }else{14 System.out.println("status === " +status );15 System.out.println("请求失败!");16 }17 }18 }
4、运行主类的主方法
原文:https://www.cnblogs.com/h--d/p/13060688.html