流程:
- 系統(tǒng)啟動(dòng)蕴茴,掃描到api工程下的Swagger2Configuration配置類(lèi)
- 配置類(lèi)中指定了包路徑com.xuecheng,這會(huì)找到包下被@RestController注解的類(lèi)
- 根據(jù)controller的swagger注解生成接口文檔
在本項(xiàng)目中因惭,把類(lèi)注解和方法注解到controller實(shí)現(xiàn)的接口上,
至于為什么注解到接口上绩衷,就可以根據(jù)實(shí)現(xiàn)類(lèi)生成文檔蹦魔,理解方式兩種:框架內(nèi)部處理(應(yīng)該是這種方式) 注解繼承了(待)
Swagger2Configuration.java
package com.xuecheng.api.config;
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 Swagger2Configuration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xuecheng"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("學(xué)成網(wǎng)api文檔")
.description("學(xué)成網(wǎng)api文檔")
// .termsOfServiceUrl("/")
.version("1.0")
.build();
}
}
CmsPageControllerApi.java
package com.xuecheng.api.cms;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.model.response.QueryResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(value = "cms頁(yè)面管理接口",description = "cms頁(yè)面管理接口激率,提供頁(yè)面的CRUD")
public interface CmsPageControllerApi {
@ApiOperation("分頁(yè)管理頁(yè)面列表")
@ApiImplicitParams({
@ApiImplicitParam(name = "page",value = "頁(yè)碼",required = true,paramType = "path",dataType = "int"),
@ApiImplicitParam(name = "size",value = "每頁(yè)記錄數(shù)",required = true,paramType = "path",dataType = "int")
})
QueryResponseResult findPageList(int page, int size, CmsPage cmsPage);
}
cmsPageController.java
@RestController
@ComponentScan("com.xuecheng.api")
public class CmsPageController implements CmsPageControllerApi {
@Autowired
private CmsPageService cmsPageService;
@GetMapping("/cms/page/{page}/{size}")
@Override
public QueryResponseResult findPageList(@PathVariable("page") int page,@PathVariable("size") int size, CmsPage cmsPage) {
QueryResponseResult result = cmsPageService.findnCmsPageList(page, size, cmsPage);
return result;
}
@GetMapping("test")
@ResponseBody
public String test() {
System.out.println("Test");
return "ok";
}
}