springboot中使用swagger2构建restful接口文档
2017-10-13 14:57
761 查看
最近几年互联网项目、移动应用越来越多,不同于之前的企业内部应用,该类项目通常就是好几个应用互相调用,我们一般会使用word或者excel来记录接口的相应描述,但是这样会有一些问题,比如更新困难,不易管理。
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件,它既能解决帮我们自动生成接口文档,并且还能在线调试。
记录下在springboot中swagger的使用
1、添加依赖
2、编写配置类
3、编写实体类
4、编写restful接口
运行项目,输入http://localhost:8080/swagger-ui.html
如果能看到该页面,则证明整合成功了。
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件,它既能解决帮我们自动生成接口文档,并且还能在线调试。
记录下在springboot中swagger的使用
1、添加依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2、编写配置类
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 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { Contact contact=new Contact("meepoguan","http://blog.csdn.net/u011890101","邮箱地址"); return new ApiInfoBuilder() .title("接口文档") .description("测试接口") .termsOfServiceUrl("测试接口") .contact(contact) .version("1.0") .build(); } }
3、编写实体类
public class User implements Serializable{ /** * */ private static final long serialVersionUID = 3221700752972709820L; private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } }
4、编写restful接口
@RestController @RequestMapping("users") public class UserController { @ApiOperation(value="获取用户列表") @GetMapping public String list() { //TODO 查询操作 return ""; } @ApiOperation(value="新增用户",notes="填写对象创建用户") @ApiImplicitParam(name="user",value="实体user",required=true,dataType="User") @PostMapping public String saveUser(@RequestBody User user) { //TODO 新增操作 return ""; } @ApiOperation(value="更新用户",notes="根据id指定对象,并根据传来的对象进行更新") @ApiImplicitParams({ @ApiImplicitParam(name="id",value="用户id",required=true,dataType="String"), @ApiImplicitParam(name="user",value="用户实体对象",required=true,dataType="User") }) @PutMapping(value="{id}") public String updateuser(@PathVariable String id) { //TODO 更新用户 return ""; } @ApiOperation(value="删除用户") @ApiImplicitParam(name="id",value="用户id",required=true,dataType="String") @DeleteMapping(value="id") public String deleteUser(@PathVariable String id) { //TODO 删除用户 return ""; } }
运行项目,输入http://localhost:8080/swagger-ui.html
如果能看到该页面,则证明整合成功了。
相关文章推荐
- Spring Boot 2.0.0.M7 中使用Swagger2构建RESTful API文档
- 在spring-boot中使用swagger2来构建RESTful API文档
- Springboot中使用Swagger2构建RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建RESTful APIS(含源码)
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- springboot集成swagger2构建RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- springboot集成swagger2构建RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建RESTful APIs