Swagger

 

Swagger

1.优点

  • API文档生成可以在线调试(postman)
  • 直接运行,可以测试api接口
  • 支持多种语言

2.官网

swagger官网

3.使用

   这里采用spring boot集成swagger

  3.1 pom的导入

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger2</artifactId>
 <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger-ui</artifactId>
 <version>2.9.2</version>
</dependency>

 3.2编写测试

@GetMapping("/hello")
 @ResponseBody
 public String hello(){
     return "hello world";
 }

     配置config

@Configuration
@EnableSwagger2   // 开启swagger2
public class SwaggeCofig {
 
}

  在主启动类上加上

 

@ComponentScan(basePackages = {"com.jtr.ajaxdemo.config"})

  访问http://127.0.0.1/swagger-ui.html

   如果出现以下情况就说明配置成功swagger

  3.3 详细配置swagger

    就是详细配置docker

     

@Configuration
@EnableSwagger2   // 开启swagger2
@ComponentScan("com.jtr.ajaxdemo.controller")
public class SwaggeCofig {
      //配置 swagger的docke 的bean信息
      @Bean
      public Docket docket(Environment environment){
        

          return new Docket(DocumentationType.SWAGGER_2). apiInfo(apiInfo()).groupName("joer").select().
                  apis(RequestHandlerSelectors.basePackage("com.jtr.ajaxdemo.controller")).
                  build();

      }
      public ApiInfo apiInfo(){
          //作者信息
          Contact DEFAULT_CONTACT = new Contact("joker", "http://jjj", "2069335440@qq.com");
       return new ApiInfo("开开发文档", "自测开发中", "1.0", "http://www.baidu.com", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());

      }

}

  3.4  配置接口扫描和开关详解

@Bean
   public Docket docket(){
       return new Docket(DocumentationType.SWAGGER_2).select().
 //RequestHandlerSelectors.basePackage() 扫描包下                  basePackage("com.jtr.ajaxdemo.controller")
               //any() 扫描所有
               //none() 不扫描任何   
               //withMethodAnnotation 方法上的注解
                //withClassAnnotation() 类上的注解        
              apis(RequestHandlerSelectors.basePackage("com.jtr.ajaxdemo.controller")).
                       //过滤到请求路径
                       paths(PathSelectors.ant("/kuang")) .
                       build().
                       apiInfo(apiInfo());
   }

3.5 配置是否启动

生成环境下关闭swagger

spring.profiles.active=dev

    //配置 swagger的docke 的bean信息
      @Bean
      public Docket docket(){
          return new Docket(DocumentationType.SWAGGER_2). apiInfo(apiInfo()).enable(false).select().
                  apis(RequestHandlerSelectors.basePackage("com.jtr.ajaxdemo.controller")).
                         /* //过滤到请求路径
                          paths(PathSelectors.ant("/kuang")) .*/
                          build();

      }

4.java中获取配置文件值

1.Properties

 FileInputStream fileInputStream=new FileInputStream("F:\\ajaxdemo\\src\\main\\resources\\application.properties");
        Properties properties=new Properties();
        properties.load(fileInputStream);
        String name = properties.getProperty("name");

2.Environment

  @Autowired
    private Environment env;
    String name = env.getProperty("name");

5.分组(group)

只要配置多个docker就行

  @Bean
      public Docket docket(Environment environment) {
          Profiles profiles=Profiles.of("dev","test");
          boolean b = environment.acceptsProfiles(profiles);

          System.out.println(b);

          return new Docket(DocumentationType.SWAGGER_2). apiInfo(apiInfo()).groupName("joer").select().
                  apis(RequestHandlerSelectors.basePackage("com.jtr.ajaxdemo.controller")).
                  build();

      }
      @Bean
      public Docket docket1(){
          return  new Docket(DocumentationType.SWAGGER_2). apiInfo(apiInfo()).groupName("lihuizhi");
      }  

6.配置Model

//model描述
@ApiModel("用户类")
public class User {
   @ApiModelProperty("用户id")
    private Integer  id;
    private String username;
    private String password;
    public User(){

    }
 }   

@PostMapping("/user")
    @ApiOperation("控制类") // controller上的中文注解
    public User user(){
        return new User();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值