zoukankan      html  css  js  c++  java
  • 【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、运行主类的主方法

        

  • 相关阅读:
    用LazyLoad实现Javascript脚本与Css样式文件延迟加载
    SQL Server2008中删除重复记录
    数据访问层的单元测试
    Asp.net使用HttpModule压缩并删除空白Html请求
    Lazy<T>的应用之类内部字段与C#延迟加载
    Asp.net MVC3 RTM的源代调试
    Asp.net MVC 3 防止 CrossSite Request Forgery (CSRF)原理及扩展
    初识JVM
    JVM虚拟机栈简介
    Tomcat处理HTTP请求源码分析(上)
  • 原文地址:https://www.cnblogs.com/h--d/p/13060688.html
Copyright © 2011-2022 走看看