springBoot+Swagger 蔚落 2021-12-16 12:33 356阅读 0赞 1.pom 依赖 <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> .2.config bean 注入 package com.yt.file_manager.sys.config; import com.google.common.base.Predicates; import io.swagger.annotations.Api; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.VendorExtension; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.Arrays; import java.util.HashSet; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(Predicates.and( RequestHandlerSelectors.basePackage("com.yt.file_manager.fastdfs"), RequestHandlerSelectors.withClassAnnotation(Api.class)) ) .build() .useDefaultResponseMessages(false) .consumes(new HashSet<>(Arrays.asList("application/json"))) .produces(new HashSet<>(Arrays.asList("application/json"))); } private ApiInfo apiInfo() { return new ApiInfo( "资源服务中心Swagger_Api协议文档", "资源服接口Api接口协议", "1.0", "https://blog.csdn.net/xinpz", new Contact("cnzz", "https://blog.csdn.net/xinpz", "https://blog.csdn.net/xinpz"), "cnzz博客", "https://blog.csdn.net/xinpz", Arrays.asList(new VendorExtension[] {})); } } 3.探针注解 @Api(tags = "fastDFS接口服务") @Controller @RequestMapping("/fastdfs") public class FileObjectController {} @ApiOperation(value = "服务启动测试") @RequestMapping(value="/test",method = RequestMethod.GET) @ResponseBody public FileResponseData test(){ return new FileResponseData(true); } @ApiOperation(value = "上传图片",notes="默认只能上传 ['png', 'gif', 'jpeg', 'jpg'] 几种类型") @ApiImplicitParam(name = "file", value = "图片", required = true, dataType = "MultipartFile") @RequestMapping(value="/upload/image/sample",method = RequestMethod.POST) @ResponseBody public FileResponseData uploadImageSample(@RequestParam MultipartFile file, HttpServletRequest request){} @ApiOperation(value = "上传图片",notes="默认只能上传 ['png', 'gif', 'jpeg', 'jpg'] 几种类型") @ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"), @ApiImplicitParam(name = "channel", value = "渠道", required = true, dataType = "String") }) @RequestMapping(value="/upload/image/sample",method = RequestMethod.POST) @ResponseBody public FileResponseData uploadImageSample(@RequestParam MultipartFile file,String channel, HttpServletRequest request){} 4.请求客户端路径 [http://localhost:8081/swagger-ui.html\#! ][http_localhost_8081_swagger-ui.html] 结果展示 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpbnB6_size_16_color_FFFFFF_t_70][] [http_localhost_8081_swagger-ui.html]: http://localhost:8081/swagger-ui.html#!%C2%A0 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpbnB6_size_16_color_FFFFFF_t_70]: /images/20211214/8858fd85902148db89e04614c4ae6101.png
还没有评论,来说两句吧...