spring cloud(五):Swagger2的集成

1、概念

   Swagger2 – 服務接口文檔,用來前端與後端聯繫,提高之間的溝通,更有效率的完成項目之間的聯調。

2、集成

  2.1 、通過pom文件導入swagger

           <dependency>

             <groupId>swagger-springmvc</groupId>

             <artifactId>swagger-springmvc</artifactId>

             </dependency>

 2.2、建立swagger模塊,編寫模塊類

         @Configuration

        @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訪問

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章