springboot集成Swagger遇到的问题

springboot集成Swagger2以及解决遇到的问题

  1. springboot集成Swagger-ui

    • swagger2 官网:https://swagger.io/

    • 在pom.xml添加依赖

      
              <dependency>
                  <groupId>io.springfoxgroupId>
                  <artifactId>springfox-swagger2artifactId>
                  <version>2.9.2version>
              dependency>
              <dependency>
                  <groupId>io.springfoxgroupId>
                  <artifactId>springfox-swagger-uiartifactId>
                  <version>2.9.2version>
              dependency>
              <dependency>
                  <groupId>com.google.guavagroupId>
                  <artifactId>guavaartifactId>
                  <version>25.1-jreversion>
              dependency>
      
    • swagger2配置类

      /**
      Swagger2配置类
      */
      
      @Configuration  //让Spring容器加载该配置类
      @EnableSwagger2 //用于启动swagger2
      public class SwaggerConfig {
      /**
      swagger2启动后,通过createRestApi函数常见Docker的Bean,
      apiInfo()用来创建该APi的基本信息(这些基本信息会展现在文档页面中)
      select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,
      这里使用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)
      */
          @Bean
          public Docket createRestApi() {
              return new Docket(DocumentationType.SWAGGER_2)
                      .apiInfo(apiInfo())
                      .select()
                      .apis(RequestHandlerSelectors.basePackage("{扫描的使用swagger2的接口的包}"))
                      .paths(PathSelectors.any())
                      .build();
          }
      
          private ApiInfo apiInfo() {
              return new ApiInfoBuilder()
                      .title("Spring Boot中使用Swagger2构建RESTful APIs")
                      .description("摘要")
                      .termsOfServiceUrl("http://www.baidu.com/")
                      .contact("peter")
                      .version("1.0")
                      .build();
          }
      
       /*   @Override
          public void addResourceHandlers(ResourceHandlerRegistry registry) {
      
              registry.addResourceHandler("swagger-ui.html")
                      .addResourceLocations("classpath:/META-INF/resources/");
              registry.addResourceHandler("/webjars*")
                      .addResourceLocations("classpath:/META-INF/resources/webjars/");
              super.addResourceHandlers(registry);
          }*/
      }
      
    • swagger2使用

       /**
           * 根据产品id和卖家id查询查询asin数据
           *
           * @return
           */
          @ApiOperation(value = "根据产品id查询asin数据", notes = "根据产品id查询asin数据")
          @ApiImplicitParam(name = "productId", value = "产品id")
          @RequestMapping(value = "/findAsinByProductId", method = RequestMethod.GET)
          public ResultBean findAsinByProductIdAndSellerId(@RequestParam("productId") Integer productId) {
              ResultBean resultBean = new ResultBean();
              try {
      
                  List<String> asins = productCodedInformationService.findAsinByProductId(productId);
                  resultBean.setData(asins);
              } catch (Exception e) {
                  resultBean.setCode(StatusCode.HTTP_FAILURE);
                  resultBean.setMsg("Request ProductCodedInformationHandler list Failed!");
                  LOGGER.error("查询asin列表失败!", e);
              }
              return resultBean;
          }
      
    • 成功后
      springboot集成Swagger遇到的问题_第1张图片

  2. 出现的问题
    访问swagger-ui.html不显示内容
    解决方式:

    //不显示内容的话需要配置静态资源路径
    /**
     * @ClassName UploadFilePathConfig
     * @Description TODO
     * @Author peter
     * @Date 19-4-15 下午5:41
     * @Version 1.0
     **/
    @Configuration
    public class UploadFilePathConfig extends WebMvcConfigurationSupport {
    	//这两个是我开放上传的路径
        @Value("${file.staticAccessPath}")
        private String staticAccessPath;
        @Value("${file.uploadFolder}")
        private String uploadFolder;
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler(staticAccessPath)
                    .addResourceLocations("file:" + uploadFolder);
            //下面两个是配置swagger的静态资源访问路径
            registry.addResourceHandler("swagger-ui.html*")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
    }
    

你可能感兴趣的:(SpringBoot,异常解决)