【Java】開發一個Java-SDK


前提

  實際上開發一個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 <project xmlns="http://maven.apache.org/POM/4.0.0"
 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     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>org.example</groupId>
 8     <artifactId>test-sdk-java</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <dependencies>
12         <dependency>
13             <groupId>cn.hutool</groupId>
14             <artifactId>hutool-all</artifactId>
15             <version>5.3.7</version>
16         </dependency>
17     </dependencies>
18 
19 </project>

  3、編輯封裝類,可以封裝一些必要的邏輯

    MyHttpRequest.java

 1 package com.test.api.bean;
 2 
 3 import cn.hutool.http.HttpRequest;
 4 import cn.hutool.http.HttpResponse;
 5 
 6 public class MyHttpRequest {
 7 
 8     private HttpRequest httpRequest;
 9 
10     public MyHttpRequest(String url){
11         httpRequest = HttpRequest.get(url);
12         // 可以封裝其他邏輯
13     }
14 
15     public MyHttpResponse execute(){
16         if(httpRequest == null) {
17             throw new NullPointerException("httpRequest is empty !");
18         }
19         HttpResponse httpResponse = httpRequest.execute();
20         MyHttpResponse myHttpResponse = new MyHttpResponse();
21         myHttpResponse.setHttpResponse(httpResponse);
22         return myHttpResponse;
23     }
24 
25     public static void main(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 package com.test.api.bean;
 2 
 3 import cn.hutool.http.HttpResponse;
 4 
 5 public class MyHttpResponse{
 6 
 7     HttpResponse httpResponse;
 8 
 9     public HttpResponse getHttpResponse() {
10         return httpResponse;
11     }
12 
13     public void setHttpResponse(HttpResponse httpResponse) {
14         this.httpResponse = httpResponse;
15 
16     }
17 
18     public int getStatus() {
19         if(httpResponse == null) {
20             throw new NullPointerException("httpResponse is empty !");
21         }
22         return httpResponse.getStatus();
23     }
24 
25     public String body(){
26         if(httpResponse == null) {
27             throw new NullPointerException("httpResponse is empty !");
28         }
29         return httpResponse.body();
30     }
31 }

  4、編輯一個客戶端MyHttpClient.java

 1 package com.test.api;
 2 
 3 import com.test.api.bean.MyHttpRequest;
 4 import com.test.api.bean.MyHttpResponse;
 5 
 6 public class MyHttpClient {
 7 
 8     private static MyHttpClient myHttpClient;
 9 
10     /**
11      * @return  Instance
12      */
13     public static MyHttpClient getInstance() {
14         synchronized (MyHttpClient.class) {
15             if(myHttpClient == null) {
16                 myHttpClient = new MyHttpClient();
17             }
18         }
19         return myHttpClient;
20     }
21 
22     public MyHttpResponse execute(String url){
23         MyHttpRequest myHttpRequest = new MyHttpRequest(url);
24         return myHttpRequest.execute();
25     }
26 
27 }

  5、打包

    1)調用方是maven工程,調用時直接使用依賴引入的方法使用sdk

    使用mvn命令:mvn clean install打包安裝

    2)調用方是非maven工程,調用時直使用 sdk jar包

    需要使用其他方式打包:見【Java】Maven 打包可運行jar包

使用Java-SDK

  1、新建一個Maven工程test-sdk-java-test  

  2、引入sdk依賴

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 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     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>org.example</groupId>
 8     <artifactId>test-sdk-java-test</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <dependencies>
12         <dependency>
13             <groupId>org.example</groupId>
14             <artifactId>test-sdk-java</artifactId>
15             <version>1.0-SNAPSHOT</version>
16         </dependency>
17     </dependencies>
18 </project>

  3、編寫主類進行測試

 1 public class MyMain {
 2     public static void main(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、運行主類的主方法

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM