springboot集成swagger2,构建优雅的Restful API

1. 引入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2. 创建Swagger2配置类

在Application.java同级创建Swagger2的配置类Swagger2。

@Configuration
@EnableSwagger2
public class Swagger2 {
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.fukaiit.controller"))
				.paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("springboot集成swagger2,构建优雅的Restful API")
				.description("springboot集成swagger2,构建优雅的Restful API,https://blog.csdn.net/fukaiit")
				.termsOfServiceUrl("https://blog.csdn.net/fukaiit")
				.version("1.0")
				.build();
	}
}

3. 编写生成文档的注解

都有什么常用注解?这里有详细说明:
https://blog.csdn.net/u014231523/article/details/76522486

例:

@RestController
@RequestMapping("/swagger2")
public class Swagger2TestController {

	@ApiOperation(value = "获取一个列表的方法", notes = "获取一个列表的方法说明")
	@GetMapping("/getList")
	public String getList() {
		return "获取一个列表";
	}

	@ApiOperation(value = "创建一个项目的方法", notes = "创建一个项目的方法说明")
	@ApiImplicitParam(name = "itemName", value = "需要新创建的项目", required = true, dataType = "string")
	@PostMapping("/createItem")
	public String createItem(@RequestBody String itemName) {
		return "创建了一个item";
	}
}

通过相关注解,就可以让swagger2生成相应的文档。如果你不需要某接口生成文档,只需要在加@ApiIgnore注解即可。需要说明的是,如果请求参数在url上,@ApiImplicitParam 上加paramType = “path” 。

4. 查看接口文档

启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot集成Swagger是为了更方便地生成API文档,使得API文档更加规范、易读和易于维护。 Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。Spring Boot是一个快速开发框架,集成了大量的开箱即用的功能,能够帮助开发者快速地构建应用。 在Spring Boot中集成Swagger,需要引入相应的依赖,配置Swagger相关的注解和配置信息。通过访问Swagger UI页面,可以方便地查看API文档、测试API接口等。 通过Spring Boot集成Swagger,我们可以更好地管理和维护API文档,提高开发效率和代码质量。 ### 回答2: Spring Boot可以通过集成Swagger来自动生成API文档。Swagger是一个规范和工具集,用于设计、构建和维护RESTful风格的API文档。以下是集成Swagger的步骤: 1. 在pom.xml文件中添加Swagger依赖: ``` <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.3.1</version> </dependency> ``` 2. 创建一个Swagger配置类,使用@EnableSwagger2注解启用Swagger: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 3. 使用Swagger注解描述API接口。在Controller类或方法上添加@Api、@ApiOperation等注解来描述API的信息、请求和响应参数等。 4. 运行Spring Boot应用程序,并访问"http://localhost:8080/swagger-ui.html",可以看到自动生成的API文档页面。 集成Swagger可以方便地为API接口生成文档,并且可以在页面上进行测试。开发人员和客户端可以根据这些文档了解API的使用方式和参数要求,减少沟通成本,提高开发效率。 ### 回答3: Spring Boot是用于构建Java应用程序的开源框架,而Swagger是用于设计、构建和文档化RESTful API的工具。将Swagger集成到Spring Boot项目中,可以方便地生成API文档,并提供交互式API文档浏览界面。 首先,需要在项目的pom.xml文件中添加Swagger依赖。可以使用以下代码片段添加Swagger的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 添加完依赖之后,需要创建一个配置类来启用Swagger。可以创建一个名为SwaggerConfig的类,并使用@EnableSwagger2注解启用Swagger。 ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 在这个配置类中,可以通过api()方法来配置Swagger的一些参数,例如扫描的包路径、API路径等。 配置完成后,可以通过访问http://localhost:8080/swagger-ui.html来查看Swagger生成的API文档。在这个界面上,可以查看每个API的详细信息,包括请求参数、响应类型等。同时,还提供了测试API的功能,方便进行接口的调试和测试。 需要注意的是,集成Swagger只是在项目中添加了API文档的功能,实际的API实现还需要编写具体的业务逻辑代码。 综上所述,通过上述步骤,我们可以将Swagger集成到Spring Boot项目中,并生成具有交互性的API文档界面,方便开发和测试人员查阅和测试API接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值