SpringBoot实战之12 整合restful工具swagger2
2017-12-05 22:21
711 查看
前言
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
code实现
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency>
启用swagger2
新建SwaggerConfig.java@Configuration //说明此类是一个配置类 @EnableSwagger2 //启用swagger2 public class SwaggerConfig { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.hsy.springboot.swagger.web")) .paths(PathSelectors.any()) .build() ; } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("spirngboot利用swagger构建api文档") .description("简单优雅的restfun风格,https://github.com/shiyuan2he/springboot") .termsOfServiceUrl("https://github.com/shiyuan2he/springboot") .version("1.0") .build(); } }
新建实体Bean作为查询实体
public class Book { private long id; private String name; private double price; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
新建BookController
/** * Api:修饰整个类,描述Controller的作用 ApiOperation:描述一个类的一个方法,或者说一个接口 ApiParam:单个参数描述 ApiModel:用对象来接收参数 ApiProperty:用对象接收参数时,描述对象的一个字段 ApiResponse:HTTP响应其中1个描述 ApiResponses:HTTP响应整体描述 ApiIgnore:使用该注解忽略这个API ApiError :发生错误返回的信息 ApiParamImplicitL:一个请求参数 ApiParamsImplicit 多个请求参数 */ @Api(value = "书籍接口",description = "书籍相关的处理") @RestController @RequestMapping("/api/books") public class BookController { Map<Long, Book> books = Collections.synchronizedMap(new HashMap<Long, Book>()); @ApiOperation(value="获取图书列表", notes="获取图书列表") @RequestMapping(value={""}, method= RequestMethod.GET) public List<Book> getBook() { List<Book> book = new ArrayList<>(books.values()); return book; } @ApiOperation(value="创建图书", notes="创建图书") @ApiImplicitParam(name = "book", value = "图书详细实体", required = true, dataType = "Book") @RequestMapping(value="", method=RequestMethod.POST) public String postBook(@RequestBody Book book) { books.put(book.getId(), book); return "success"; } @ApiOperation(value="获图书细信息", notes="根据url的id来获取详细信息") @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path") @RequestMapping(value="/{id}", method=RequestMethod.GET) public Book getBook(@PathVariable Long id) { return books.get(id); } @ApiOperation(value="更新信息", notes="根据url的id来指定更新图书信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long",paramType = "path"), @ApiImplicitParam(name = "book", value = "图书实体book", required = true, dataType = "Book") }) @RequestMapping(value="/{id}", method= RequestMethod.PUT) public String putUser(@PathVariable Long id, @RequestBody Book book) { Book book1 = books.get(id); book1.setName(book.getName()); book1.setPrice(book.getPrice()); books.put(id, book1); return "success"; } @ApiOperation(value="删除图书", notes="根据url的id来指定删除图书") @ApiImplicitParam(name = "id", value = "图书ID", required = true, dataType = "Long",paramType = "path") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) public String deleteUser(@PathVariable Long id) { books.remove(id); return "success"; } @ApiIgnore//使用该注解忽略这个API @RequestMapping(value = "/hello", method = RequestMethod.GET) public String jsonTest() { return "world!"; } }
新建项目入口SpringBootSwaggerApplication.java
@SpringBootApplication public class SpringBootSwaggerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSwaggerApplication.class,args) ; } }
项目结构图
运行效果图
历史文章
SpringBoot实战之入门springboot实战之文章汇总
springboot实战之读取配置文件
springboot实战之整合jsp模版引擎
springboot实战之整合freemarker模版引擎
springboot实战之注册自定义Servlet
springboot实战之注册filter和listener
springboot实战之注册interceptor
springboot实战之整合slf4j日志系统
springboot实战之整合CommandLineRunner
相关文章推荐
- springboot-restful api-swagger2 ui-实战
- springboot-restful api-swagger2 ui-实战
- springboot-restful api-swagger2 ui-实战
- SpringBoot实战之10 整合jdbc进行crud操作
- SpringBoot+Maven项目实战(3):整合Freemark模板
- [置顶] spring boot项目实战之工具篇(json)
- [置顶] spring boot项目实战之工具篇(json)
- springboot集成swagger2构建RESTful API文档
- Spring Boot 2.0.3 JDBC整合Oracle 12
- 整合阿里dubbo服务的项目搭建实战(springboot)
- Springboot中使用Swagger2构建RESTful API文档
- [Spring Boot实战系列] - No.3 Spring boot 整合Mybatis
- 在spring-boot中使用swagger2来构建RESTful API文档
- spring boot实战(第十四篇)整合RabbitMQ源码分析前言
- spring cloud-整合Swagger2构建RESTful服务的APIs
- springboot+rabbitMq整合开发实战一
- [置顶] spring boot项目实战之工具篇(json)
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
- springboot集成swagger2构建RESTful API文档