整合Swagger接口文档

本文档介绍了如何通过Springfox库在Spring Boot项目中集成Swagger,实现接口文档的自动生成。步骤包括添加Swagger依赖,配置Swagger属性,创建Swagger配置类,并展示了如何避免接口方法未指定HTTP方法导致的问题。
摘要由CSDN通过智能技术生成

Swagger接口文档:自动生成接口文档
1.添加依赖:

<!-- 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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

2.添加配置

swagger:
  title: xxx项目RESTful API
  description: 开发人员太懒,没有写描述
  version: 1.0
  contactName: lxt
  contactEmail:
  contactUrl:
  basePackageRest: com.qcby.xxx.rest
  termsOfServiceUrl:

3.建一个properties包,建一个Swagger类
在这里插入图片描述

@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties {
    private String title;
    private String contactName;
    private String contactUrl;
    private String contactEmail;
    private String version;
    private String description;
   private String basePackageRest;
   private String termsOfServiceUrl;
   .... get  set方法
}

4.在config里创建SwaggerConfig配置类
在这里插入图片描述

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private SwaggerProperties swaggerProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("REST接口")
                .apiInfo(apiInfo())
                .select()
                //  配置自动扫描那些包下类型生成接口文档
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackageRest()))
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title(swaggerProperties.getTitle())
                //创建人
                .contact(new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(),swaggerProperties.getContactEmail()))
                //版本号
                .version(swaggerProperties.getVersion())
                //描述
                .description(swaggerProperties.getDescription())
                .build();
    }
}

注意:
1.
在这里插入图片描述

如果我们不给controller层的接口加get、post请求方法,那么会出现这么一种情况:
在这里插入图片描述
一个方法会有多种请求方式,当接口非常多的时候就会很多,所以我们需要加上请求方法,post方法:@Postmapping
get、post方法:@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值