springboot 整合 swagger 以及 创建自动配置的jar包

  1. 引入相应的jar包

    <!-- API获取的包 -->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
    </dependency>
    <!-- 官方UI包 -->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
    </dependency>
    <!-- 测试数据以JSON格式返回的依赖包 -->
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
    </dependency>
    
  2. 写一个swagger的配置类

    @Configuration
    @EnableSwagger2
    @Profile({"dev", "sit"})
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi() {
            System.out.println("swagger config 生效了");
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title("SpringBoot整合Swagger")
                            .description("SpringBoot整合Swagger,详细信息......")
                            .version("1.0")
    //                        .license("The Apache2.0")
    //                        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                            .build());
        }
    }
    
  3. 编写对应的Controller

    @Api(tags = "test")
    @RestController
    @RequestMapping("/test")
    public class TestController {
        @Autowired
        private TestService testService;
    
        @ApiOperation("测试运行")
        @GetMapping("/demo")
        public String test() {
            return "hello world";
        }
    
        @ApiOperation("测试运行2")
        @GetMapping("/demo2")
        public String test2(String name) {
            return name + "hello world";
        }
    
        @ApiOperation("测试dubbo使用")
        @GetMapping("/dubbo")
        public String dubbo() {
            return testService.testDemo();
        }
    }
    
  4. 如果真正的项目编写还需要将swagger的地址设置成无须安全校验即可访问。

父工程配置类

  1. 父工程配置类之所以能够生效是因为目录结构与子工程相同,因此在执行ComponentScan时会识别到启动类同目录下或其子类下的配置类。

组件jar包引入使用解决方案

  1. 友好度从高到低排序

    1. springboot 主动发现,配置一个resources/META-INF/spring.factories 文件,boot中有程序会读取该文件执行自动配置。

      org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      com.baoyong.base.demo.BaseDemoConfig
      

      org.springframework.boot.autoconfigure.EnableAutoConfiguration 代表自动配置的 key, 即代表需要自动配置哪些类;

      \ 可以理解为一个换行符,则该行下面的每行当做一个参数;

      第二行则为配置类的全路径,如果需要 Spring 自动配置多个类,依行写入它的全路径即可

    2. 使用者通过注解方式启用配置:

      1. 生产者工程创建一个统一的组件入口配置类,创建配置文件读取目录下的配置类。

        @Configuration
        @ComponentScan("com.baoyong.base.demo.**"})
        public class BaseDemoConfig{
        }
        
      2. 生产者工程创建一个注解提供给用户使用,因此需要创建一个注解:

        @Retention(RetentionPolicy.RUNTIME)
        @Target({ElementType.TYPE})
        @Documented
        // 该注解引入了前面编写的配置类,让该类正常生效
        @Import({BaseDemoConfig.class})
        public @interface EnableBaseDemo {
        }
        
      3. 用户使用如下,当启动程序时,前面的BaseDemoConfig就会被注册到bean,其本身的ComponentScan也会正常生效。

        @EnableLogRecordClient
        @Configuration
        public class ConsumerConfig {
        }
        
    3. 使用者手动配置basePackages,这要求使用者熟悉知道配置文件路径;自创建项目根路径一般相同,无须配置也可以正常使用。

      @Configuration
      @ComponentScan(basePackages = {"com.baoyong.base.demo.**"})
      public class ConsumerConfig{
      
      }
      
  2. 根据情况使用方案1或2,目前最适合springboot的个人认为是方式1,用户可以无须配置直接引入使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值