特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/
1、问题描述
随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。 前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger
就是一款让你更好的书写API文档的框架,而且swagger可以完全模拟http请求,入参出参和实际情况差别几乎为零。
没有API文档工具之前,大家都是手写API文档的(维护起来相当困难),在什么地方书写的都有,有在confluence
上写的,有在对应的项目目录下readme.md
上写的,每个公司都有每个公司的玩法,无所谓好坏。但是能称之为“框架”的,估计也只有swagger
了
2、操作步骤
2.1配置pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- spring boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 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> </dependencies>
2.2启动类
package com.mao.swagger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @EnableAutoConfiguration @SpringBootApplication(scanBasePackages = "com.mao") public class DemoApp { public static void main(String[] args) throws Exception { SpringApplication.run(DemoApp.class, args); } }
2.3配置config
package com.mao.swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import io.swagger.annotations.ApiOperation; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build(); } }
2.4配置controller
package com.mao.swagger.controller; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.mao.swagger.beans.ResObject; import com.mao.swagger.beans.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * Hello world! * */ @Api(description = "用户接口") @RestController @RequestMapping("/demoController") public class DemoController { @ApiOperation(value = "新增用户" , notes="新增注册") @RequestMapping(value="/createUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE) public ResObject createUser(@RequestBody User user){ System.out.println("createUser:::"+user.toString()); return new ResObject(HttpStatus.OK.value(), "新增成功."); } @ApiOperation(value = "修改用户" , notes="修改用户") @RequestMapping(value="/updateUser",method=RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE) public ResObject updateUser(@RequestBody User user){ System.out.println("updateUser:::"+user.toString()); return new ResObject(HttpStatus.OK.value(), "修改成功."); } @ApiOperation(value = "删除用户" , notes="删除用户") @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value="/deleteUser",method=RequestMethod.DELETE) public ResObject deleteUser(@RequestParam("userId") String userId){ System.out.println("deleteUser:::"+userId); return new ResObject(HttpStatus.OK.value(), "删除成功."); } @ApiOperation(value = "查询用户" , notes="查询用户") @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value="/queryUser",method=RequestMethod.GET) public ResObject queryUser(@RequestParam("userId") String userId){ System.out.println("queryUser:::"+userId); User user = new User(userId, "张三", "******", "mao2080@sina.com"); return new ResObject(HttpStatus.OK.value(), user); } }
3、效果展示
按照上面的启动之后访问:http://localhost:8080/swagger-ui.html 发现找不到接口
后面排查是因为没有添加扫描包
重启之后再刷新即可
点击demo-controller可以看到详细接口
点击具体接口可以看到具体参数
点击try it out! 可以测试接口。
后台打印日志
4、附件下载
5、参考网站
https://blog.csdn.net/i6448038/article/details/77622977
https://blog.csdn.net/blackmambaprogrammer/article/details/72354007