springboot集成Swagger2以及解决遇到的问题
-
springboot集成Swagger-ui
-
swagger2 官网:https://swagger.io/
-
在pom.xml添加依赖
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>25.1-jre</version> </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; }
-
成功后
-
-
出现的问题
访问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); } }