spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)

时间:2023-03-08 18:03:30
spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)

一,为什么要集成外部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目录下 

项目结构如图:

spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)

三,项目中配置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)