配置Swagger2
生活随笔
收集整理的這篇文章主要介紹了
配置Swagger2
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
創(chuàng)建common模塊
在guli-parent下創(chuàng)建模塊common配置:
groupId:com.leon
artifactId:common?
在common中引入相關(guān)依賴
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>provided </scope></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><scope>provided </scope></dependency><!--lombok用來(lái)簡(jiǎn)化實(shí)體類:需要安裝lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided </scope></dependency><!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><scope>provided </scope></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><scope>provided </scope></dependency><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- spring2.X集成redis所需common-pool2<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.6.0</version></dependency>--> </dependencies>在common下面創(chuàng)建子模塊service-base
在模塊service-base中,創(chuàng)建swagger的配置類
創(chuàng)建包c(diǎn)om.leon.servicebase.config,創(chuàng)建類SwaggerConfig
package com.leon.servicebase;import com.google.common.base.Predicates; 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.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration//配置類 @EnableSwagger2 //swagger注解 public class SwaggerConfig {@Beanpublic Docket webApiConfig(){return new Docket(DocumentationType.SWAGGER_2).groupName("webApi").apiInfo(webApiInfo()).select().paths(Predicates.not(PathSelectors.regex("/admin/.*"))).paths(Predicates.not(PathSelectors.regex("/error.*"))).build();}private ApiInfo webApiInfo(){return new ApiInfoBuilder().title("網(wǎng)站-課程中心API文檔").description("本文檔描述了課程中心微服務(wù)接口定義").version("1.0").contact(new Contact("java", "http://leon.com", "1123@qq.com")).build();} }在模塊service模塊中引入service-base
<dependency><groupId>com.leon</groupId><artifactId>service-base</artifactId><version>0.0.1-SNAPSHOT</version> </dependency>在service-edu啟動(dòng)類上添加注解,進(jìn)行測(cè)試
@SpringBootApplication @ComponentScan(basePackages = {"com.leon"}) public class EduApplication {public static void main(String[] args) {SpringApplication.run(EduApplication.class, args);} }API模型
可以添加一些自定義設(shè)置,例如:
定義樣例數(shù)據(jù)
@ApiModelProperty(value = "創(chuàng)建時(shí)間", example = "2019-01-01 8:00:00") @TableField(fill = FieldFill.INSERT) private Date gmtCreate;@ApiModelProperty(value = "更新時(shí)間", example = "2019-01-01 8:00:00") @TableField(fill = FieldFill.INSERT_UPDATE) private Date gmtModified;定義接口說(shuō)明和參數(shù)說(shuō)明
定義在類上:@Api
定義在方法上:@ApiOperation
定義在參數(shù)上:@ApiParam
@Api(description="講師管理") @RestController @RequestMapping("/admin/edu/teacher") public class TeacherAdminController {@Autowiredprivate TeacherService teacherService;@ApiOperation(value = "所有講師列表")@GetMappingpublic List<Teacher> list(){return teacherService.list(null);}@ApiOperation(value = "根據(jù)ID刪除講師")@DeleteMapping("{id}")public boolean removeById(@ApiParam(name = "id", value = "講師ID", required = true)@PathVariable String id){return teacherService.removeById(id);}}?
總結(jié)
以上是生活随笔為你收集整理的配置Swagger2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Swagger2介绍
- 下一篇: 统一返回数据格式