SpringBoot框架 之 Druid与Swagger2

目录

  • Druid
    • Druid连接池配置
    • Druid数据监控
  • 集成Swagger2
    • Swagger2简介
    • 1.添加依赖
    • 2.创建Swagger2配置类
    • 3.在控制器方法上添加对应api信息
    • 4.启动Spring boot,访问Swagger UI界面
    • 常见Api

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

安装依赖


    com.alibaba
    druid
    1.1.6



    org.projectlombok
    lombok



    log4j
    log4j
    1.2.17

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 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 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.添加依赖


    io.springfox
    springfox-swagger2
    2.2.2


    io.springfox
    springfox-swagger-ui
    2.2.2

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)