您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swagger springboot