首页 > 编程语言 > 详细

spring cloud(五):Swagger2的集成

时间:2018-04-09 11:41:19      阅读:320      评论:0      收藏:0      [点我收藏+]
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", "", "test@163.com", "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的集成

原文:http://blog.51cto.com/xxdeelon/2095893

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!