swagger配置类

@Configuration
@EnableSwagger2  // swagger类添加这个注解
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包下的controller生成api文档
                .apis(RequestHandlerSelectors.basePackage("com.lh.server.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        //设置文档信息
        return new ApiInfoBuilder()
                .title("项目接口文档")
                .description("项目接口文档")
                .contact(new Contact("xxxx", "http:localhost:8081/doc.html",
                        "xxxx@xxxx.com"))
                .version("1.0")
                .build();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Swagger来生成API文档时,你需要编写Swagger配置来定义API的基本信息、请求参数、响应模型等。下面是一个使用Java编写Swagger配置的示例: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("your.package.name")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Your API Title") .description("Your API Description") .version("1.0.0") .build(); } } ``` 在上面的示例中,我们使用了Springfox框架来集成Swagger。`@Configuration`注解表示该是一个配置,`@EnableSwagger2`注解表示启用Swagger。 `@Bean`注解用于创建一个`Docket`实例,用于配置Swagger的基本信息。在`Docket`的构造方法中,我们指定了Swagger的版本为2.0,并使用`.apiInfo()`方法来配置API的基本信息,如标题、描述和版本号。 `.select()`方法用于指定要生成API文档的接口所在的包路径,通过`.apis(RequestHandlerSelectors.basePackage("your.package.name"))`来设置。 `.paths(PathSelectors.any())`表示选择所有的接口路径生成API文档。 最后,使用`.build()`方法来构建`Docket`实例。 你可以根据自己的项目需求修改上述示例代码中的配置信息,然后将该配置添加到你的项目中,即可生成Swagger API文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值