SpringBoot框架 之 Druid与Swagger2
Druid
Druid连接池配置
spring:
mvc:
servlet:
load-on-startup: 1
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,去掉后监控界面sq1无法统计,’wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
安装依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Druid数据监控
访问地址
http://localhost/druid
@Configuration
public class DruidConfig{
@ConfigurationProperties(prefix="spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//1.配置servlet
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
HashMap<Object, Object> hashMap = new HashMap<> ();
hashMap.put("loginUsername", "admin");
hashMap.put("loginPassword", "123456");
hashMap.put("allow", "");//允许访问所有
bean.setInitParameters(hashMap);
return bean;
}
//2.配置Filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean=new FilterRegistrationBean(new WebStatFilter());
HashMap<Object,Object> hashMap=new HashMap<>();
hashMap.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(hashMap);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
集成Swagger2
Swagger2简介
1.随项目自动生成强大RESTful API文档,减少工作量
2.API文档与代码整合在一起,便于同步更新API说明
3.页面测试功能来调试每个RESTful API
1.添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.创建Swagger2配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.itlike"))// 指定扫描包下面的注解
.paths(PathSelectors.any())
.build();
}
// 创建api的基本信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("集成Swagger2构建RESTful APIs")
.description("集成Swagger2构建RESTful APIs")
.termsOfServiceUrl("https://www.baidu.com")
.contact("itlike")
.version("1.0.0")
.build();
}
}
3.在控制器方法上添加对应api信息
@Api(value="用户controller",tags={"用户操作接口"})
@RequestMapping("hero")
public class MyController{
@Autowired
private HeroService heroService;
@ApiOperation(value="获取英雄信息",notes="根据id来获取英雄详细信息)
@ApiImplicitParam(name="id",value="用户ID",required=true,dataType="String")
@RequestMapping("/getHero/{id}")
@ResponseBody
public Hero getHero(@PathVariable("id")Long id,ModelMap modelMap){
Hero hero=heroService.getHeroById(id);
modelMap.addAttribute("hero",hero);
return hero;
}
}
4.启动Spring boot,访问Swagger UI界面
http://localhost/swagger-ui.html#/
常见Api
@Api(value="用户controller",tags={"用户操作接口"})
Api 用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源
@ApiOperation(value="获取用户信息",notes="注意问题点",httpMethod="GET")
用在方法上,说明方法的作用,每一个url资源的定义,使用方式
@ApiImplicitParams({@ApiImplicitParam(name="id",value="用户id",dataType="Long", paramType = "path")})
参数说明
@ApiIgnore()
忽略方法
SpringBoot框架 之 Druid与Swagger2的更多相关文章
- SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)
一.引入依赖 引入数据库连接池的依赖--druid和面向切面编程的依赖--aop,如下所示: <!-- druid --> <dependency> <groupId&g ...
- SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...
- Spring-Cloud之Spring-Boot框架-1
一.Spring Boot 是由 Pivotal 团队开发的 Spring 框架,采用了生产就绪的观点 ,旨在简化配置,致力于快速开发. Spring Boot 框架提供了自动装配和起步依赖,使开发人 ...
- Springboot框架
本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...
- SpringBoot 框架整合
代码地址如下:http://www.demodashi.com/demo/12522.html 一.主要思路 使用spring-boot-starter-jdbc集成Mybatis框架 通过sprin ...
- SpringBoot框架:两个方法同时调用时父方法使内部方法的DataSource注解失效的解决办法
一.问题如下: 使用的是SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)中的两个数据库spring_boot_demo和other_data. 在UserCo ...
- Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器
Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器 Mybatis-plus官网: https://mp.baomidou.com/ Demo ...
- Springboot 框架学习
Springboot 框架学习 前言 Spring Boot是Spring 官方的顶级项目之一,她的其他小伙伴还有Spring Cloud.Spring Framework.Spring Data等等 ...
- 小程序后端项目【Springboot框架】部署到阿里云服务器【支持https访问】
前言: 我的后端项目是Java写的,用的Springboot框架.在部署服务器并配置https访问过程中,因为做了一些令人窒息的操作(事后发现),所以老是不能成功. 不成功具体点说就是:域名地址可以正 ...
随机推荐
- DataPipeline如何实现数据质量管理?
数据质量管理已经成为数据治理的重要组成部分.高质量的数据是企业进行决策的重要依据. DataPipeline数据质量平台整合了数据质量分析.质量校验.质量监控等多方面特性, 以保证数据质量的完整性.一 ...
- PHP java时间戳转php时间戳
/** * java时间戳转php时间戳 * @param int $javaUt java的时间戳 * @return int * @Date 2019/8/26 */ public static ...
- 大数据之kafka-02.搞定kafka专业术语
02.搞定kafka专业术语 在kafka的世界中有很多概念和术语是需要我们提前理解并且熟练掌握的,下面来盘点一下. 之前我们提到过,kafka属于分布式的消息引擎系统,主要功能是提供一套完善的消息发 ...
- Python Image库简单处理图像
直接列举几个常用的函数,可在 http://effbot.org/imagingbook/image.htm 中查看更多相关函数. from PIL import Image import numpy ...
- Tomcat--安装部署
Tomcat安装部署 Tomcat简介 官网:http://tomcat.apache.org/ Tomcat服务器是一个免费的开源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问 ...
- DB开发规范---初稿
1 公共约定 1.1 存储引擎 默认统一使用InnoDB引擎 1.2 字符集设定 后续新建DB默认使用utf8mb4字符集,校对规则使用utf8mb4_general_bin. 历史DB多使用utf8 ...
- Caused by SSLError("Can’t connect to HTTPS URL because the SSL module is not available)
window7系统: 今天刚安装的anaconda(开源的Python包管理器),把原来的python3和python2都给卸载了,结果运行爬虫程序的时候报错: Caused by SSLError( ...
- iptables 规则学习
iptables 一共有 3 张表:mangle,nat,filter mangle 表主要处理 ttl,tos,mark 等信息(进) filter 顾名思义就是过滤器,用作防火墙(出) nat 主 ...
- YII2 使用curl请求,返回false
一. 起因: 今天用yii框架,请求java接口,始终返回false. 二. 分析历程: 使用curl_error()方法打印出Peer’s Certificate issuer is not rec ...
- 删除WordPress菜单wp-nav-menu中li的class或id样式
我们都知道wordpress已经集成了一些通用的css样式,比如wp-nav-menu菜单会有很多的class,不想看到那么多的选择器,想要清净的世界要如何操作呢?随ytkah一起来看看 <li ...