日常工作总结--Swagger工作的使用

本文介绍了Swagger所需的jar包,分享了亲测过的2.2版本Swagger2配置文件。同时详细列举了swagger常用注解,包括controller层的@Api、@ApiOperation等,以及pojo类的@ApiModel、@ApiModelProperty等注解的使用方法和参数含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swagger所需的jar包

       <!--swagger2相关-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

Swagger2配置文件:亲实过2.2版本

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = {
        "com.***.controller"
        }) //扫描的包名
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("Authorization").description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("token")
                .required(true)
                .build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档大标题")
                .version("1.0")
                .description("接口文档小标题")
                .build();
    }

}

 swagger常用注解

controller层:

   @Api(descriptio="控制层描述")  类名上(或者tage="" 前面备注)

   @ApiOperation(value="方法描述",response="方法返回")

@ApiParam(name="参数名",value="参数说明",required="是否必须")

@Apilgnore //用于取消swagger页面显示

pojo类

@ApiModel(value="对象名",descriptio="对象描述")

@ApiModelProperty(value="model属性说明",name="重写属性名",dataType="重写属性类型",example="举例说明",hidden="true为隐藏",required="是否必填")


@ApiImplicitParams(name="参数ming",value="参数说明",  dataType="数据类型",paramType="参数类型" ,example="举例说明" )   用于方法,包含多个 @ApiImplicitParam 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值