springboot整合Swagger2
2019-02-28 13:46
411 查看
现在公司中项目几乎都是前后端分离, 前端和后端的唯一联系,变成了API接口,以前用postman来测试后台接口,刚刚接触了Swagger,觉得是一个不错的自测工具。接下来我们就开始整合。
1.jar包的引入
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency>
2.swagger2的配置文件
在项目的启动类的同级文件建立
package com.example.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; @Configuration @EnableSwagger2 public class Swagger2 { //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.example.spring")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("Spring Boot 测试使用 Swagger2 构建RESTful API") //创建人 .contact(new Contact("xiaohong", "http://www.baidu.com", "")) //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }
3.编写controller层
package com.example.spring.controller; import com.example.spring.model.User; import com.example.spring.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api") @Api("swaggerDemoController相关的api") public class SwaggerDemoController { @Autowired private UserService userService; private static final Logger logger= LoggerFactory.getLogger(SwaggerDemoController.class); @ApiOperation(value = "根据id查询用户信息", notes = "查询数据库中某个的用户信息") @ApiImplicitParam(name = "id", value = "UserID", paramType = "path", required = true, dataType = "Long") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User getStudent(@PathVariable Long id) { logger.info("开始查询某个用户信息"); return userService.selectByPrimaryKey(id); } }
再写一个
package com.example.spring.controller; import com.example.spring.model.User; import com.example.spring.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration @RequestMapping("/testboot") public class TestController { @Autowired UserService userService; @RequestMapping("getuser") public User getUser() { User user = new User(); user.setName("test"); return user; } @PostMapping("add") public int addUser(@RequestBody User user){ return userService.insert(user); } }
这样swagger2与springboot就集成完毕了。
访问 http://localhost:8080/swagger-ui.html 看一下最终结果
发现出来了两个咱们刚刚写的controller
点开
出现查询结果,如下
是不是很方便,我们不用像postman一样来编写入口,swagger2自动完成
而且实时更新
相关文章推荐
- SpringBoot之整合Swagger2
- Spring Boot整合Swagger2
- Springboot整合swagger2项目的部署问题
- SpringBoot整合Swagger2
- springboot整合swagger2企业项目搭建
- spring boot整合Swagger2的示例代码
- Spring Boot整合Swagger2的完整步骤详解
- SpringBoot整合Swagger2,再也不用维护接口文档了!
- Spring Boot整合Swagger2
- spring boot 整合swagger2 实现动态生成接口文档
- Swagger(一) SpringBoot整合Swagger2简单的例子
- SpringBoot整合Swagger2
- SpringBoot和Swagger2整合的小demo
- SpringBoot(七):SpringBoot整合Swagger2
- SpringBoot整合Swagger2
- SpringBoot(七):SpringBoot整合Swagger2
- spring boot 整合Swagger2
- Swagger(一) SpringBoot整合Swagger2简单的例子
- 二、SpringBoot 整合 swagger2 (swagger2 版本 2.8.0)
- Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结