springboot集成swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。1、配置pom.xml
作用:
- 接口的文档在线自动生成。
- 功能测试。
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!-- jackson相关依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artifactId>classmate</artifactId> <version>1.2.0</version> </dependency> 2、使用注解来进行启动swagger import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author Administrator */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 //.apis(RequestHandlerSelectors.basePackage("com.template.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); // return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } /** * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个 * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("SpringBoot 使用 Swagger2 构建RESTful API") //创建人 .contact(new Contact("Test", "http://www.xuanqi.co/", "")) //版本号 .version("1.0") //描述 .description("API帮助文档") .build(); } }
3、配置Controller
@Slf4j @RestController @Api(description = "测试接口") @RequestMapping("test") public class TestController extends BaseController { @ApiOperation(value = "测试方法",notes = "返回多个测试结果") @ApiImplicitParam(name="param",value="测试参数",paramType ="查询",required = true, dataType = "String") @GetMapping("constant") public String getConstant(String param){ Map<String,String> map=new HashMap<>(); map.put("threadName",Thread.currentThread().getName()); map.put("param",param); log.info(JsonUtil.toJson(map)); return JsonUtil.toJson(map); } }
4、页面效果
http://localhost:9001/web/swagger-ui.html
5、controller测试效果
。
相关推荐
莫问前程 2020-06-22
莫问前程 2020-06-14
SAMXIE 2020-04-17
pontuss 2020-04-09
smalllove 2020-03-27
meleto 2020-03-05
SAMXIE 2020-11-04
XuDanT 2020-09-16
permanent00 2020-09-15
哈嘿Blog 2020-09-08
Qizonghui 2020-08-02
莫问前程 2020-08-02
SAMXIE 2020-07-26
XuDanT 2020-07-24
莫问前程 2020-07-18
Qizonghui 2020-07-18
coolhty 2020-07-05
Qizonghui 2020-06-28
Qizonghui 2020-06-25