使用Swagger生成Spring Boot微服务API文档

栏目: 服务器 · 发布时间: 7年前

内容简介:Swagger是一个开源框架,可以在将你的Restful API文档化,供其他访问者浏览,包括应该提交的JSON格式,获得响应JSON格式等。首先在Spring Boot的pom.xml中引入swagger2包支持:springfox是产生API文档,而swagger-ui 则是RestAPI的界面。

Swagger是一个开源框架,可以在将你的Restful API文档化,供其他访问者浏览,包括应该提交的JSON格式,获得响应JSON格式等。

首先在Spring Boot的pom.xml中引入swagger2包支持:

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>

springfox是产生API文档,而swagger-ui 则是RestAPI的界面。

创建一个RestController ,定义API:


@RestController
@Api(value="/customer",description="Customer Profile",produces ="application/json")
@RequestMapping(
"/customer")
public class CustomerController {
@ApiOperation(value=
"get customer",response=Customer.class)
@ApiResponses(value={
@ApiResponse(code=200,message=
"Customer Details Retrieved",response=Customer.class),
@ApiResponse(code=500,message=
"Internal Server Error"),
@ApiResponse(code=404,message=
"Customer not found")
})
@RequestMapping(value=
"/getCustomer",method=RequestMethod.GET,produces="application/json")
public ResponseEntity<Customer> getCustomer(){
Customer cust = new Customer();
cust.setName(
"Sagar");
cust.setId(1234);
cust.setAddress(
"Pune");
return new ResponseEntity<Customer>(cust, HttpStatus.OK);
}
}

这里特殊的地方就是多了很多新的注释:

1. @Api 定义这个控制器是什么

2. @ApiOperation 定义请求方法

3. @ApiResponses 定义方法可能返回的所有响应。

下面需要激活Swagger的配置:


@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket produceApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hotel.main.controllers"))
.paths(paths())
.build();
}
// Describe your apis
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(
"Hotel Management Rest APIs")
.description(
"This page lists all the rest apis for Hotel Management App.")
.version(
"1.0-SNAPSHOT")
.build();
}
// Only select apis that matches the given Predicates.
private Predicate<String> paths() {
// Match all paths except /error
return Predicates.and(
PathSelectors.regex(
"/customer.*"),
Predicates.not(PathSelectors.regex(
"/error.*"))
;
}
}

@EnableSwagger2 是在应用启动时激活swagger ;

Docket - 它是一个构建器,在swagger-springmvc框架中充当主要接口。它的构建字段如下:

apiInfo - 它返回一个ApiInfoBuilder,它指定Rest API的标题,描述等。

select()- 它返回ApiSelectorBuilder的一个实例 ,它提供了一种控制Swagger公开的端点的方法。

apis - 提供RequestHandlerSelectors,它指定basepackage来扫描所有控制器。

paths() - 提供API的映射端点。

最后是启动类:


@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法分析

数据结构与算法分析

(美)(C.A.谢弗)Clifford A.Shaffer / 电子工业出版社 / 1998-8 / 35.00元

本书综合“数据结构与算法”的知识梳理、习题解答及上机辅导等于一身;精心挑选了覆盖教学大纲的五百多道题目,并且提供所有题目的参考答案;对于较难的算法和上机题,给出了详细的分析和说明;对于学习的重点和难点、易犯的错误、题目的难易和重要性,以及国内教材的差异等都给出了必要的说明。 本书可给使用各种教材讲授和学习“数据结构与算法”(或者“数据结构”)的师生参考,是系统复习该课程和准备应考计算......一起来看看 《数据结构与算法分析》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具