Spring Boot中使用Swagger2构建RESTful APIS(含源码)
来源:互联网 发布:中国核心期刊数据库 编辑:程序博客网 时间:2024/05/21 07:12
Swagger2简介
本次教程是Spring Boot中使用Swagger2构建RESTful APIS
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。(如图)
Swagger除了查看接口功能外,还提供了调试测试功能。(如图)
新增博客
查看所有博客
修改博客
查看单个博客
删除博客
SpringBoot整合Swagger2
配置pom.xml,引入Swagger2架包
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version></dependency>
在RunApplication.java同级下创建Swagger2.java
package com.javazhan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.*;import springfox.documentation.service.*;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;/** * Created by yando on 2017/11/10. */@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIS") .description("HTTP对外开放接口") .version("1.0.0") .termsOfServiceUrl("http://blog.csdn.net/wenteryan") .license("Spring Boot 入门+实战(提供源码哟)") .licenseUrl("http://blog.csdn.net/column/details/15021.html") .build(); }}
创建实体类 Blog.java(略)
创建ABlogController.java
package com.javazhan.controller.admin;import com.javazhan.domain.Blog;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.*;import java.util.*;/** * Created by yando on 2017/11/13. */@RestController@RequestMapping(value = "/admin/blog")public class ABlogController { static Map<Long, Blog> blogs = Collections.synchronizedMap(new HashMap<Long, Blog>()) ; @ApiOperation(value = "后台管理查询所有博客") @RequestMapping(value = "", method = RequestMethod.GET) public List<Blog> getBlogList() { List<Blog> list = new ArrayList<Blog>(blogs.values()) ; return list ; } @ApiOperation(value = "新增博客", notes = "根据博客对象") @ApiImplicitParam(name = "blog", value = "博客信息实体blog", required = true, dataType = "Blog") @RequestMapping(value = "/add", method = RequestMethod.POST) public Map addBlog(@RequestBody Blog blog) { blogs.put(blog.getId(), blog) ; Map map = new HashMap() ; map.put("message", "新增成功") ; map.put("code", "0000") ; return map ; } @ApiOperation(value = "获取博客信息", notes = "根据ID获取博客对象") @ApiImplicitParam(name = "id", value = "博客ID", required = true, paramType="path", dataType = "Long") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Blog getBlogById(@PathVariable Long id) { Blog blog = new Blog() ; blog = blogs.get(id) ; return blog ; } @ApiOperation(value = "修改博客信息", notes = "根据传过来的博客对象修改博客信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "博客ID", required = true, paramType="path", dataType = "Long"), @ApiImplicitParam(name = "blog", value = "博客信息实体blog", required = true, dataType = "Blog") }) @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public Map updateBlog(@PathVariable Long id, @RequestBody Blog blog) { Blog b = blogs.get(blog.getId()) ; b.setId(blog.getId()) ; b.setName(blog.getName()) ; blogs.put(b.getId(), b) ; Map map = new HashMap() ; map.put("message", "修改成功") ; map.put("code", "0000") ; return map ; } @ApiOperation(value = "删除博客信息", notes = "根据ID删除博客对象") @ApiImplicitParam(name = "id", value = "博客ID", required = true, paramType="path", dataType = "Long") @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public Map deleteBlog(@PathVariable Long id) { blogs.remove(id) ; Map map = new HashMap() ; map.put("message", "删除成功") ; map.put("code", "0000") ; return map ; }}
运行RunApplication.java
访问地址:http://localhost:8080/swagger-ui.html
更多教程请参考官方文档
源码下载
Spring Boot中使用Swagger2构建RESTful APIS(含源码)
版权声明:本文为博主原创文章,未经博主允许不得转载。转载请注明出处:http://blog.csdn.net/wenteryan
阅读全文
0 1
- Spring Boot中使用Swagger2构建RESTful APIS(含源码)
- Spring Boot中使用Swagger2构建RESTful APIs
- Spring Boot中使用Swagger2构建RESTful API文档
- Spring Boot使用Swagger2构建RESTful文档
- 使用Swagger2生成spring boot应用RESTful APIs描述文档
- Spring boot构建RESTFul API+使用Swagger2构建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 API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- http协议接口调用之WebClient
- 小型Basic编译器问题
- 惊了
- Spring Mybatis 返回List<Map<String,Object>>
- xml常用转义
- Spring Boot中使用Swagger2构建RESTful APIS(含源码)
- 分布式系统的Quorum策略
- 【分布式】分布式事务的一种实现方式--状态流转
- killoffer-001
- sdut 2099 小型Basic编译器问题
- CloudWatch Examples
- Android7.0中调用系统打开图片报FileUriExposedException
- SVN里常见的图标及其含义
- git-