溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring cloud(五):Swagger2的集成

發(fā)布時(shí)間:2020-07-14 12:00:46 來(lái)源:網(wǎng)絡(luò) 閱讀:3821 作者:browser123 欄目:軟件技術(shù)

1、概念

   Swagger2 – 服務(wù)接口文檔,用來(lái)前端與后端聯(lián)系,提高之間的溝通,更有效率的完成項(xiàng)目之間的聯(lián)調(diào)。

2、集成

  2.1 、通過(guò)pom文件導(dǎo)入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="新增用戶(開發(fā)責(zé)任人:xx)")

          @ApiServiceOperation("新增用戶接口")

            @RequestMapping(value={"/xx/user/add"}, method={RequestMethod.POST})

          @ResponseBody

          public Response<Integer> createSoftsimBinding(@RequestBody SoftsimBindingCreateReq req)

          {

         }

4、導(dǎo)入swagger相關(guān)的ui到項(xiàng)目里面,可以建static文件夾存放swagger的ui文件


5、通過(guò)地址http://localhost:8020/swagger/index.html訪問

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI