spring cloud(五):Swagger2的集成

发布时间:2020-07-14 12:00:46 作者:browser123
来源:网络 阅读:3811

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访问

推荐阅读:
  1. 详解微服务:Spring Cloud原理及核心
  2. 从 Spring Cloud 看一个微服务框架的「五脏六腑」

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

swagger java spring cloud

上一篇:邮件阅读状态跟踪

下一篇:客户端被DDOS怎么办?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》