spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)
2023-09-14 09:01:19 时间
一,为什么要集成外部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)
相关文章
- spring-boot打包,引入外部jar包问题
- 把spring-boot项目部署到tomcat容器中
- 非spring的 jar 包里创建一个bean,但是不自动引入(代码库)
- [Spring Boot] Introduce to Mockito
- spring boot:在项目中引入第三方外部jar包集成为本地jar包(spring boot 2.3.2)
- Spring的jar包详解
- spring boot:分别在jar内部和外部使用配置文件(spring boot v2.5.4)
- spring boot:rabbitmq用topic模式发送接收消息(spring boot 2.4.4)
- 从头开始搭建一个Spring boot+RabbitMQ环境
- Spring MVC更多家族成员--国际化视图与LocalResolver---10
- Inside Spring - learning notes - Jerry Wang的Spring学习笔记
- 毕业设计 Spring Boot的疫情防控物业管理系统
- Spring官网下载各版本jar包
- Spring MVC资源绑定视图解析器
- spring cloud :五、分布式配置中心(spring cloud config)
- Spring目录结构和基础JAR包介绍
- 12、笔记-spring组成
- 【Spring Boot】SpringBoot项目jar、war包启动解析