SpringBoot整合Swagger

依赖

<!--swagger 文档注释-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--swagger-->

Swagger的Configuration

扫描指定接口
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                //扫描指定包下的所有接口
                .apis(RequestHandlerSelectors.basePackage("hello.spring.cloud.service.controller"))
                //扫描所有接口
                //.apis(RequestHandlerSelectors.any())
                //扫描拥有指定类注解下的所有接口
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                //不扫描
                //.apis(RequestHandlerSelectors.none())
                //扫描拥有指定方法注解下的所有接口
                //.apis(RequestHandlerSelectors.withMethodAnnotation(PostMapping.class)
                .build();
    }
}
扫描指定请求
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                //扫描指定包下的所有接口
                .apis(RequestHandlerSelectors.basePackage("hello.spring.cloud.service.controller"))
                //指定只扫描user请求下的所有请求
                .paths(PathSelectors.ant("/user/**"))
                .build();
    }
}

指定忽略的接口参数
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .ignoredParameterTypes(HttpSession.class);
    }
}
指定swagger在哪种环境下可以展示
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    Environment environment;
    @Bean
    public Docket docket(){
        Profiles profiles = Profiles.of("dev","test");
        boolean isEnable = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(isEnable);
    }
}

//application.yml下指定环境
spring:
  profiles:
    active: pro
API进行分组展示
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    Environment environment;
    @Bean
    public Docket docketUser(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户")
                .select()
                .paths(PathSelectors.ant("/user/**"))
                .build();
    }
    @Bean
    public Docket docketTest(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("测试")
                .select()
                .paths(PathSelectors.ant("/test/**"))
                .build();
    }
}
实体信息配置
//给实体起注释,在swagger界面可展示
@ApiModel("用户实体")
public class User {
    //为实体属性起注释
    @ApiModelProperty("用户名")
    String name;
    @ApiModelProperty("用户密码")
    String password;
    @ApiModelProperty("年龄")
    Integer age;
全局参数配置
@Configuration
@EnableSwagger2
public class SwaggerConfig { ;
    @Bean
    public Docket docket(){
        Parameter build = new ParameterBuilder().name("token")
                .description("用户登录令牌")
                //header:参数在请求头,必须在接口参数上写@RequestHeader("token") Object obj
                //query:参数在请求体,不需要在接口参数做任何处理
                .parameterType("header")
                //参数类型
                .modelRef(new ModelRef("String"))
                //true:必须要填这个token才能访问这个接口
                .required(true)
                .build();
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(build);
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(parameters);
    }
}

描述接口信息
@Api(tags = "用户相关的请求")//描述请求信息
@RestController
@RequestMapping("/user")
public class SwaggerTestController {

    @ApiOperation("获取用户信息")//描述接口信息
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名"),
            @ApiImplicitParam(name = "password",value = "用户密码")
    })
    @GetMapping("/getUser")
    public User getUser(String name,String password){
        User user = new User("张三","123456",18);
        return user;
    }
    @PostMapping("/setUser")
    public String setUser(@RequestBody User user){
        return user.getName();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值