1. 程式人生 > 實用技巧 >spring boot:在專案中引入第三方外部jar包整合為本地jar包(spring boot 2.3.2)

spring boot:在專案中引入第三方外部jar包整合為本地jar包(spring boot 2.3.2)

一,為什麼要整合外部jar包?

不是所有的第三方庫都會上傳到mvnrepository,

這時我們如果想整合它的第三方庫,則需要直接在專案中整合它們的jar包,

在操作上還是很簡單的,

這裡用luosimao的簡訊平臺sdk演示一下

說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest

對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/

說明:作者:劉巨集締 郵箱: [email protected]

二,下載並新增到專案

1,下載jar包, 共兩個檔案: jersey-bundle-1.19.jar json-org.jar 2,在專案的resources目錄下建立jar目錄, 把兩個jar包檔案複製到resources/jar目錄下

專案結構如圖:

三,專案中配置pom.xml

配置pom.xml

1,增加jar的依賴
        <!--luosimao send sms begin-->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>api</artifactId>
            <version>1.19</version>
            <scope>
system</scope> <systemPath>${project.basedir}/src/main/resources/jar/jersey-bundle-1.19.jar</systemPath> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <
version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/jar/json-org.jar</systemPath> </dependency> <!--luosimao send sms end-->
說明:因為是本地的jar包,不需要作為從maven的倉庫裡中拉取庫檔案時的依據 所以groupId/artifactId/version 自己按情況隨意填寫即可 2,以plugin增加includeSystemScope 它的用途是讓maven打包時把我們新增的外部jar包也打包時去 否則maven打包時會漏掉我們手動新增的jar
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
效果如下:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build> 

四,用官方的例子新增一段java程式碼,並測試效果

1,HomeController.java

@RestController
@RequestMapping("/home")
public class HomeController {

    //查詢剩餘簡訊條數狀態
    @GetMapping("/status")
    public String status() {
        //Api api = new Api();
        String httpResponse =  testStatus();
        try {
            JSONObject jsonObj = new JSONObject( httpResponse );
            int error_code = jsonObj.getInt("error");
            if( error_code == 0 ){
                int deposit = jsonObj.getInt("deposit");
                System.out.println("Fetch deposit success :"+deposit);
            }else{
                String error_msg = jsonObj.getString("msg");
                System.out.println("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
            }
        } catch (JSONException ex) {
            //Logger.getLogger(Api.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        return httpResponse;
    }

    //傳送並返回對狀態的查詢
    private String testStatus(){
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                "api","key-thisisakeydomoforlaoliutest"));
        WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        ClientResponse response =  webResource.get( ClientResponse.class );
        String textEntity = response.getEntity(String.class);
        int status = response.getStatus();
        return textEntity;
    }
}

官方的demo程式碼,我們整合到controller中測試效果

2,測試效果

訪問:

http://127.0.0.1:8080/home/status

返回:

{"error":0,"deposit":"88888888"}

返回了剩餘的簡訊條數,說明我們對外部jar包的整合是有效的

六,檢視spring boot版本

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)