一,為什么要集成外部jar包?
不是所有的第三方庫都會上傳到mvnrepository,
這時我們如果想集成它的第三方庫,則需要直接在項目中集成它們的jar包,
在操作上還是很簡單的,
這里用luosimao的短信平台sdk演示一下
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,下載並添加到項目
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)