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访问过程中,因为做了一些令人窒息的操作(事后发现),所以老是不能成功. 不成功具体点说就是:域名地址可以正 ...
随机推荐
- springCloud学习1(集中式配置管理)
springcloud 总集:https://www.tapme.top/blog/detail/2019-02-28-11-33 一.前言 在开发普通的 web 应用中,我们通常是将配置项写在单 ...
- Resource interpreted as Document but transferred with MIME type application/json
转自:https://blog.csdn.net/just_lover/article/details/81207472 我在修改并保存后,界面返回提示“undifine”,实际我是看到有返回提示的. ...
- day 03 作业 预科
目录 作业 1.简述变量的组成 2.简述变量名的命名规范 3.简述注释的作用 4.使用turtle库构造一幅图,贴在markdown文档中 作业 1.简述变量的组成 变量由变量名.赋值符号.变量值所组 ...
- BIND 主从配置
BIND 主从配置 环境:master:172.31.182.144slave:172.31.182.147 一.安装yum install bind bind-chroot -y 二.master ...
- golang并发基础
1. go协程(go routine) go原生支持并发:goroutine和channel. go协程是与其他函数或方法一起并发运行的函数和方法.go协程可以看作是轻量级线程. 调用函数或者方法时, ...
- MySQL/MariaDB数据库的函数
MySQL/MariaDB数据库的函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. MySQL/MariaDB数据库的函数分为系统函数和用户自定义函数(user-define ...
- ExecutorService java多线程分割list运行
调用方法 int threadNum = 7; while(true) { List<FaceAnalyseImage> list = faceAnalyseImageMapper.sel ...
- js获取ip内网地址
<script type="text/javascript"> function getUserIP(onNewIP) { // onNewIp - your list ...
- Windows解决端口被占用问题
第一种解决方法,以8080端口为例 打开命令行输入 cmd ,输入netstat -ano 会显示所有已经在运行的端口情况.PID为进程id 输入你想要查的正在占用的端口号,netstat -ano ...
- 玩转DNS服务器——Bind服务
合理的配置DNS的查询方式 实验环境: 虚拟机:VMware® Workstation 15 Pro 均使用NAT连接 网段为192.168.1.0/24 DNS 服务器 ---- Centos ...