springboot学习笔记_07_swagger2

前后端分离

  • 后端时代

    • 前端只用静态页面
    • jsp,模板引擎等后端处理开发
  • 前后端分离时代

    • 前端
      • vue/react
      • 工程化
        • 前端控制层,视图层
        • 伪造后端数据,无需后端也可直接运行
        • 调用后端API接口,json格式交互
        • 可以部署在不同的服务器
    • 后端
      • 后端控制层,服务层,数据访问层(controller,service,dao,entity)
      • springboot+mybatis
      • swagger显示API接口格式,postman工具测试

swagger

  • 号称时间上最流行的API框架
  • RestFul Api文档在线自动生成工具
  • 直接运行,在线测试API
  • 支持多种语言

官网

  • https://swagger.io
<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>
@Configuration
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
    @Bean
    //配置swagger的Docket的bean实例
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //关闭或开启swagger
                .enable(true)
                .select()
                //RequestHandlerSelectors配置要扫描的接口的方法
                //basePackage() 指定扫描的包
                //any() 扫描全部
                //none() 都不扫描
                //.apis(RequestHandlerSelectors.basePackage("web.controller"))
                //withClassAnnotation() 扫描包上的注解
                //withMethodAnnotation() 扫描方法上的注解
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //过滤
                //ant() 正则过滤
                //.paths(PathSelectors.none())
                .build();
    }

    //配置swagger的apiInfo
    private ApiInfo apiInfo(){
        return new ApiInfo("my Swagger",
                "Swagger Test",
                "v1.0",
                "团队网站",
                new Contact("weixin", "作者个人网站", "xx@weixin.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

@RestController
@Api("Hello")
@RequestMapping("/api")
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

  • 分组
@Bean
public Docket docket2(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .groupName("2")
            //关闭或开启swagger
            .enable(true)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .build();
}
@Bean
public Docket docket1(){
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .groupName("1")
            //关闭或开启swagger
            .enable(true)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .build();
}
  • 实体类
@ApiModel("用户类")
public class User {

    @ApiModelProperty("用户名")
    private String userName;
    @ApiModelProperty("密码")
    private Integer sex;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }
}
//只要接口中存在实体类,就会扫描到
@PostMapping("/user")
public User user(){
    return new User();
}
  • 访问
    • http://localhost:8080/swagger-ui.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值