spring cloud(五):Swagger2的集成
Swagger2 – 服務接口文檔,用來前端與後端聯系,提高之間的溝通,更有效率的完成項目之間的聯調。
2、集成
2.1 、通過pom文件導入swagger
<dependency>
<groupId>swagger-springmvc</groupId>
<artifactId>swagger-springmvc</artifactId>
</dependency>
2.2、建立swagger模塊,編寫模塊類
@EnableWebMvc
@EnableSwagger
@ComponentScan(basePackages={"com.sz.xx"})
public class ApiSwaggerConfig extends WebMvcConfigurerAdapter{
private SpringSwaggerConfig springSwaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(new String[] { "/xx/.*?" })
.apiVersion("0.0.1")
.swaggerGroup("api");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("XX API接口文檔", "API-DOC", "", "[email protected]", "My License", "My Apps API License URL");
return apiInfo;
}
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
{
configurer.enable();
}
class GtPaths extends SwaggerPathProvider {
GtPaths() {
}
protected String applicationPath() {
return "/restapi";
}
protected String getDocumentationPath()
{
return "/restapi";
}
}
}
}
3、代碼裏面使用swagger
3.1、UserContoller
@Api(value="userApi", description="user管理接口", position=1)
@Controller
@ApiService
public class UserContoller{
@ApiOperation(notes="新增用戶接口", value="新增用戶(開發責任人:xx)")
@ApiServiceOperation("新增用戶接口")
@RequestMapping(value={"/xx/user/add"}, method={RequestMethod.POST})
@ResponseBody
public Response<Integer> createSoftsimBinding(@RequestBody SoftsimBindingCreateReq req)
{
}
4、導入swagger相關的ui到項目裏面,可以建static文件夾存放swagger的ui文件
5、通過地址http://localhost:8020/swagger/index.html訪問
spring cloud(五):Swagger2的集成