SpringBoot学习笔记:Swagger

一、从pom.xml引入Maven依赖

注意:不要引入太新依赖,之前遇到坑,引入3.0.0版本打开不了swagger-ui.html页面

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

二、配置Config类

  • @Configuration:标注该类为配置类
  • @EnableSwagger2:开启Swagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestfulApiDocs() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cjm.swagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger学习")
                .description("Swagger Test")
                .termsOfServiceUrl("https://blog.csdn.net")
                .contact(new Contact("chenjiaming", "https://www.baidu.com", "wscjm@outlook.com"))
                .version("1.0.0")
                .build();
    }
}

三、Controller类

  • @Api(tags = “Test”):标注在类上,用于描述类作用
  • @ApiOperation(“测试方法”):标注在方法上,用于描述方法作用
  • @ApiImplicitParams:含一或多个@ApiImplicitParam,描述参数信息
@RestController
@Api(tags = "Test")
public class TestController {

    @ApiOperation("测试方法")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "姓名")})
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test(String name){
        return "Hello "+name;
    }
}

注意: 如果tags 值设置为中文, 那么 下面的方法名点击将不能被展开,改成英文之后正常。value 值是否为中文不影响。

注解说明
@Api说明类的作用
@ApiOperation方法的说明
@ApiImplicitParams、@ApiImplicitParam方法的参数的说明;@ApiImplicitParams 用于指定单个参数的说明
@ApiParam用于方法参数,字段说明
@ApiModel用在 JavaBean 类上,说明 JavaBean 的 用途
@ApiModelProperty用在 JavaBean 类的属性上面,说明此属性的的含义
@ApiResponses、@ApiResponse用于方法返回值的说明 ;@ApiResponses 用于指定单个参数的说明
@ApiIgnore用于类或者方法上,可以不被swagger显示在页面上

四、测试

打开浏览器,地址栏输入http://localhost:8080/swagger-ui.html,页面如下,表示配置成功。在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChenBbMing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值