springboot整合Swagger

前后端分离
前端:控制层,视图层
后端:控制层,服务层,数据访问

swagger

  • 基于restful API文档在线自动生成器
  • 同步更新,
  • 直接运行,在线测试API
  • 支持多语言

spring集成swagger

版本要求

  • jdk1.8及以上
  • spring4.1.7及以上
  • Mybatis3.2.2及以上
常用注解

@Api //类描述
@ApiOperation //方法描述
@ApiParam //参数描述
@ApiModel //数据模型描述
@ApiModelProperty //数据模型属性描述

springboot整合swagger

添加依赖pom.xml

 <!-- 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>

配置application.yml

spring:
    #swagger配置
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

wangge自定义配置类

//使swagger生效
@EnableSwagger2
//swagger配置类
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //.enable(true)
                //扫描
                .select()
                //允许扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("cn.kgc.springootredis.controller"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact DEFAULT_CONTACT = new Contact("", "", "");
        return new ApiInfo("Api Documentation",
                "Api Documentation",
                "1.0",
                "urn:tos",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

创建pojo文件夹
创建实体类user:

@ApiModel("用户实体类")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    @ApiModelProperty("用户编码[非必填]")
    private String uId;
    @ApiModelProperty("用户名[必填]")
    private String userName;
    @ApiModelProperty("用户密码[必填]")
    private String password;
}

创建controller
创建Controller测试类:

@Api(value = "/TestController" ,description = "测试控制器")
@RestController
public class TestController {
    @ApiOperation(value = "请求测试路径",notes = "测试字符串数据")
   @RequestMapping(value = "/test/{uname}",method = RequestMethod.GET)
    //@PathVariable 传递单参数描述
    public String SwaggerTest(@PathVariable(value = "uname",required =false) String uname){
       return "swagger";
   }
    @ApiOperation(value = "请求测试路径",notes = "测试字符串数据")
    @PostMapping(value = "/user" )
    //@ApiParam 传递参数描述
    public User User(@ApiParam 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、付费专栏及课程。

余额充值