SpringBoot项目集成Swagger和swagger-bootstrap-ui以及常用注解使用方法
一、前言
二、SpringBoot项目集成swagger
1. 引入依赖
2. 编写配置文件
3. 启动访问页面
三、SpringBoot项目集成swagger-bootstrap-ui
1.引入依赖
2.配置资源处理规则
3.启动访问页面
四、Swagger常用注解介绍
1.Swagger2Config中相关swagger注解
2.controller中相关swagger注解
3.Model中相关swagger注解
Swagger文档在SpringBoot框架下的配置,Swagger配置登录验证
## 开启 Swagger的 Basic认证功能,默认是false
swagger:
# 是否关闭 swagger接口文档访问
# production: true
basic:
# 开启简单用户验证
enable: true
# 用户名(自由发挥)
username: xx
# 用户密码(自由发挥)
password: xx
Gradle报错Could not resolve all dependencies for configuration ‘:detachedConfiguration7‘.
修改方式有两种:
第一种:在仓库前添加关键字:allowInsecureProtocol = true
第二种:将阿里云的连接http换成https
SpringBoot升级2.4.0所出现的问题:When allowCredentials is true, allowedOrigins cannot contain the specia
跨域配置报错,将.allowedOrigins替换成.allowedOriginPatterns即可。
View Details访问swagger-ui.html 404报错一秒解决
在application.properties 加上这个
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
View Detailsjava中执行Sql语句的Statement详细用法
statement-相关概述 Statement 对象用于将 SQL 语句发送到数据库中。实际上有三种 Statement 对象,它们都作为在给定连接上执行 SQL语句的包容器:Statement、PreparedStatement(它从 Statement 继承而来)和CallableStatement(它从 PreparedStatement 继承而来)。它们都专用于发送特定类型的 SQL 语句:Statement 对象用于执行不带参数的简单 SQL 语句;PreparedStatement 对象用于执行带或不带 IN参数的预编译 SQL 语句;CallableStatement 对象用于执行对数据库已存储过程的调用。 Statement 接口提供了执行语句和获取结果的基本方法。PreparedStatement 接口添加了处理 IN 参数的方法;而CallableStatement 添加了处理 OUT 参数的方法。 有些 DBMS将已存储过程中的每条语句视为独立的语句;而另外一些则将整个过程视为一个复合语句。在启用自动提交时,这种差别就变得非常重要,因为它影响什么时候调用commit 方法。在前一种情况中,每条语句单独提交;在后一种情况中,所有语句同时提交。 1、创建 Statement 对象 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句。Statement 对象用 Connection 的方法createStatement 创建,如下列代码段中所示:
1 2 |
Connection con = DriverManager.getConnection(url, "sunny",""); Statement stmt = con.createStatement(); |
为了执行 Statement 对象,被发送到数据库的 SQL 语句将被作为参数提供给 Statement 的方法:
1 |
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROMTable2"); |
2、使用 Statement 对象执行语句 Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和execute。使用哪一个方法由 SQL 语句所产生的内容决定。 方法 executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。 方法 executeUpdate 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQLDDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。 方法 execute用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能,所以本概述后面将在单独一节中对其进行介绍。 执行语句的所有方法都将关闭所调用的 Statement 对象的当前打开结果集(如果存在)。这意味着在重新执行 Statement对象之前,需要完成对当前 ResultSet 对象的处理。 应注意,继承了 […]
View Details使用Statement 的addBatch方法批量导入数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
try { boolean auto = conn.getAutoCommit(); conn.setAutoCommit(false); Statement stat = conn.createStatement(); Iterator<String> it = sourcetime.iterator(); // System.out.println("get sourcetime iterator successfully"); while (it.hasNext()) { String st = it.next(); //System.out.println("this st*_*"+st+"*_*"); Pattern subpat = Pattern.compile("[|]"); // 创建Pattern实例 String mac_time[] = subpat.split(st); int time = Integer.parseInt(mac_time[1]); // time String mac = mac_time[0]; // System.out.println("this::::"+mac+","+gname +","+time); String sql = "insert into edges (source,target,time) values('" + mac + "','" + gname + "','" + time + "')"; stat.addBatch(sql); } stat.executeBatch(); conn.commit(); logger.info("insert "+gname+" data into DB edges finished"); conn.setAutoCommit(auto); } catch (SQLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } |
from:https://www.cnblogs.com/ivywenyuan/p/4148541.html
View Detailsjava执行SQL脚本文件
1. 在工作中很多时候需要执行一个SQL脚本文件到数据库中作为初始化数据;spring提供了一个工具类ScriptUtils,具体用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); } } |
2. 但是有时候我们的SQL脚本文件很大,甚至是几百mb,这样容易造成内存溢出的情况,因此我写了一个工具类,对SQL脚本进行拆解,然后批量执行。 这样每批量执行后,就清空缓存中的SQL,因此解决内存溢出问题。如下: 具体还没有用大数据量的脚本测试,等周一到公司再测试一下吧,哈哈哈。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); // 分批处理SQL脚本 batchExecuteSql(classPathResource, 5); } /** * SQL脚本分解执行 * @param resource SQL脚本资源 * @param batchNumber 每多少条SQL执行一次 * @throws SQLException * @throws IOException */ public void batchExecuteSql(Resource resource, int batchNumber) throws SQLException, IOException { Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); BufferedReader bufferedReader = null; try { //获取字符缓冲流 bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream())); int l; int i = 0; StringBuilder sql = new StringBuilder(); while ((l = bufferedReader.read()) != -1) { char read = (char) l; sql.append(read); if (read == ';') { // 一个完整的SQL语句 i ++; statement.addBatch(sql.toString()); if (i % batchNumber == 0) { System.out.println("每" + batchNumber + "条语句批量执行开始......"); statement.executeBatch(); statement.clearBatch(); System.out.println("每" + batchNumber + "条语句批量执行结束......"); } //清除StringBuilder中的SQL语句 sql.delete(0, sql.length()); } } if (i % batchNumber != 0) { System.out.println("执行最后不足" + batchNumber + "条语句开始!!!"); statement.executeBatch(); statement.clearBatch(); System.out.println("执行最后不足" + batchNumber + "条语句结束!!!"); } } finally { if (bufferedReader != null) { bufferedReader.close(); } if (connection != null) { connection.close(); } if (statement != null) { statement.close(); } } } } |
from:https://www.cnblogs.com/fangyan1994/p/14123592.html
View Details记一次apollo连接失败处理
服务器:centos7 apollo:1.7 开发环境:springboot2.3 1.整合
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency> |
1 2 3 4 |
app.id=dev apollo.meta=http://192.167.2.123:8080 #eureka地址 apollo.bootstrap.enable=true apollo.bootstrap.namespaces=application |
1 2 3 4 |
@SpringBootApplication @EnableApolloConfig public class ApolloServerApplication { } |
2.启动时异常
1 2 3 4 5 6 7 8 |
Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:30.617 WARN 42252 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:31.623 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:33.624 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:33.628 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 1 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:34.625 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:35.629 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 2 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:36.625 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] |
3.分析原因 http://192.168.1.26:8080 这个地址不是我eureka的地址,所以才找的时候会找不到 4.解决办法 启动的环境变量中加入: -Dapollo.configService=http://192.167.2.123:8080 将地址改为eureka的地址就好 ———————————————— 版权声明:本文为CSDN博主「weixin_38777065」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_38777065/article/details/113527235
View Detailsntpdate提示
19 Jan 10:33:11 ntpdate[29616]: no server suitable for synchronization found 这种问题从下面几个点开始验证 1.查看防火墙是否开通udp的123端口,是udp 无法使用telnet来验证,只能看iptables和硬墙是否有策略 2.如果端口开通,查看服务器端的ntp.conf文件中server是否正常,如果配置有问题,肯定无法使用 3.修改配置是否重新启动ntpd服务 4.查看服务端ntpd进程是否正常 ntpdate -d ntp服务端ip 遇到的几个问题 1.Server dropped:no data 处理办法:网络不通,检查防火墙的udp端口123是否能用,查看服务端的ntpd服务是否启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@ip-192.168.25.10 ~]# ntpdate -d 192.168.25.10 19 Jan 10:24:16 ntpdate[27838]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1) Looking for host 192.168.25.10 and service ntp host found : ip-192.168.25.10.ap-southeast-1.compute.internal transmit(192.168.25.10) transmit(192.168.25.10) transmit(192.168.25.10) transmit(192.168.25.10) transmit(192.168.25.10) 192.168.25.10: Server dropped: no data server 192.168.25.10, port 123 stratum 0, precision 0, leap 00, trust 000 refid [192.168.25.10], delay 0.00000, dispersion 64.00000 ansmitted 4, in filter 4 ▽ference time: 00000000.00000000 Mon, Jan 1 1900 8:05:43.000 originate timestamp: 00000000.00000000 Mon, Jan 1 1900 8:05:43.000 transmit timestamp: e3b0c156.2402fb9d Tue, Jan 19 2021 10:24:22.140 filter delay: 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 filter offset: 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 delay 0.00000, dispersion 64.00000 offset 0.000000 19 Jan 10:24:24 ntpdate[27838]: no server suitable for synchronization found |
2.Server dropped: strata too high 这个问题一般都是server端ntp.conf中server行配置了网络ntp的服务端url导致,可以收到ntp的包,但是延迟很高。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
1 [root@ip-192.168.25.10 ~]# ntpdate -d 192.168.25.10 2 19 Jan 10:33:32 ntpdate[29681]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1) 3 Looking for host 192.168.25.10 and service ntp 4 host found : ip-192.168.25.10.ap-southeast-1.compute.internal 5 transmit(192.168.25.10) 6 receive(192.168.25.10) 7 transmit(192.168.25.10) 8 receive(192.168.25.10) 9 transmit(192.168.25.10) 10 receive(192.168.25.10) 11 transmit(192.168.25.10) 12 receive(192.168.25.10) 13 192.168.25.10: Server dropped: strata too high 14 server 192.168.25.10, port 123 15 stratum 16, precision -24, leap 11, trust 000 16 refid [192.168.25.10], delay 0.02573, dispersion 0.00000 17 transmitted 4, in filter 4 18 reference time: 00000000.00000000 Mon, Jan 1 1900 8:05:43.000 19 originate timestamp: e3b0c382.6344a1b6 Tue, Jan 19 2021 10:33:38.387 20 transmit timestamp: e3b0c382.670289fb Tue, Jan 19 2021 10:33:38.402 21 filter delay: 0.02574 0.02573 0.02574 0.02573 22 0.00000 0.00000 0.00000 0.00000 23 filter offset: -0.01470 -0.01471 -0.01471 -0.01471 24 0.000000 0.000000 0.000000 0.000000 25 delay 0.02573, dispersion 0.00000 26 offset -0.014712 27 28 19 Jan 10:33:38 ntpdate[29681]: no server suitable for synchronization found |
解决办法就是将服务端也开启ntpdate 然后通过ntpdate链接网络url的ntp服务器,然后在ntp.conf中配置server 127.127.1.0 (记住 不是127.0.0.1 这个无效) 在打开ntpq -p 查看是否转为LOCAL了
1 2 3 4 |
[root@ip-192.168.25.10 ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *LOCAL(0) .LOCL. 8 l 11 64 17 0.000 0.000 0.000 |
正确的ntpdate -d 应该是下面的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
1 [root@ip-192.168.25.10 ~]# ntpdate -d 192.168.25.10 2 19 Jan 10:35:25 ntpdate[30125]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1) 3 Looking for host 192.168.25.10 and service ntp 4 host found : ip-192.168.25.10.ap-southeast-1.compute.internal 5 transmit(192.168.25.10) 6 receive(192.168.25.10) 7 transmit(192.168.25.10) 8 receive(192.168.25.10) 9 transmit(192.168.25.10) 10 receive(192.168.25.10) 11 transmit(192.168.25.10) 12 receive(192.168.25.10) 13 server 192.168.25.10, port 123 14 stratum 6, precision -24, leap 00, trust 000 15 refid [192.168.25.10], delay 0.02574, dispersion 0.00000 16 transmitted 4, in filter 4 17 reference time: e3b0c3eb.878bf296 Tue, Jan 19 2021 10:35:23.529 18 originate timestamp: e3b0c3f3.442fd9c7 Tue, Jan 19 2021 10:35:31.266 19 transmit timestamp: e3b0c3f3.47e920c0 Tue, Jan 19 2021 10:35:31.280 20 filter delay: 0.02574 0.02574 0.02574 0.02576 21 0.00000 0.00000 0.00000 0.00000 22 filter offset: -0.01465 -0.01465 -0.01465 -0.01466 23 0.000000 0.000000 0.000000 0.000000 24 delay 0.02574, dispersion 0.00000 25 offset -0.014652 26 27 19 Jan 10:35:31 ntpdate[30125]: adjust time server 192.168.25.10 offset -0.014652 sec |
ps:以上ip均已做脱敏处理 from:https://www.cnblogs.com/zclinux/p/14296884.html
View Details