700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 整合Swagger接口文档

整合Swagger接口文档

时间:2021-03-25 10:00:38

相关推荐

整合Swagger接口文档

Swagger接口文档:自动生成接口文档

1.添加依赖:

<!-- /artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!-- /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 APIdescription: 开发人员太懒,没有写描述version: 1.0contactName: lxtcontactEmail:contactUrl:basePackageRest: com.qcby.xxx.resttermsOfServiceUrl:

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@EnableSwagger2public class SwaggerConfig {@Autowiredprivate SwaggerProperties swaggerProperties;@Beanpublic 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})

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。