springBoot集成swagger2并使用

导入依赖

<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.9.2</version>
</dependency>
<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.9.2</version>
</dependency>

配置swagger

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {

    //配置Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
            	//页面的描述信息
                .apiInfo(apiInfo())
                //开关Swagger 默认为true开启
                .enable(true)
                //设置组名
                //若要实现多个组,则要配置多个Docket的bean实例
                .groupName("zb")
                .select()
                /*apis()配置扫描接口的方式
                  basePackage()基于包扫描
                  any()扫描全部
                  none()不扫描
                  withClassAnnotation(Controller.class)扫描类上注解
                  withMethodAnnotation(GetMapping.class)扫描方法上注解*/
                .apis(RequestHandlerSelectors.basePackage("com.zb.controller"))
                  /*paths()过滤什么路径
                  ant()只扫描这个路径下的
                  any()一个也不过滤
                  none()过滤全部
                  */
                .paths(PathSelectors.none())
                .build();
    }

    //配置Swagger信息 =>apiInfo()
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("name","url","email");

        //DIY
        return new ApiInfo(
                "Api Documentation",   //title
                "Api Documentation",  //description
                "1.0",  ///version
                "urn:tos", // termsOfServiceUrl
                contact,  //作者信息
                "Apache 2.0",  //license
                "http://www.apache.org/licenses/LICENSE-2.0", //licenseUrl
                 new ArrayList());
    }


}

接口控制类

@Api(tags = "控制类注释")
@RestController
public class HelloController {

    @ApiOperation("接口注释")
    @RequestMapping("/hello")
    public String index(){
        return "hello";
    }

    //返回值存在实体类,该实体类就会被扫描进Swagger中
    @RequestMapping("/user")
    public User user(){
        return new User();
    }

    @RequestMapping("/param")
    public String param(@ApiParam("参数注释") String param){
        return param;
    }

}

实体类

@ApiModel("实体类注释")
public class User {
    @ApiModelProperty("字段名id注释")
    private int id;
    @ApiModelProperty("字段名name注释")
    private String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值