导入java sdk_【Java】开发一个Java-SDK

本文介绍了如何开发一个Java SDK,包括创建Maven工程,封装HTTP请求,编写客户端,并提供了打包与使用的两种场景:作为Maven依赖和独立jar包。通过实例展示了如何使用SDK进行HTTP请求。
摘要由CSDN通过智能技术生成

前提

实际上开发一个Java-SDK的过程,实际上也就是开发一个基本java项目并打包成jar包的过程,然后可以被其它项目引入使用。

开发Java-SDK

本例介绍开发一个向某一数据接口发送请求并返回结果的SDK

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

6216909d35fd8e58065944c2d953cbe6.png

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、运行主类的主方法

a476117ec3a804efda6221503541e7a7.png

原文:https://www.cnblogs.com/h--d/p/13060688.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值