Swagger可以做什么
使用Swagger,所有接口的信息,都在代码里面了。代码即接口文档,接口文档即代码,即可以做到代码的迭代更新时,接口文档也能更方便及时更新,减少很多维护接口文档的工作。
Swagger的简单使用
1、jar的maven引用
<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>
2、配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author ljs
* @data 2020/06/09
*
*/
@Configuration
@EnableSwagger2
public class Swagger {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).
apiInfo(apiInfo()).
select().
apis(RequestHandlerSelectors.basePackage("top.san.mc.base.web")).
paths(PathSelectors.any()).
build();
//扫描 top.san.mc.base.web 下的所有@RequestMapper接口也就是controller层,该路径根据自己的包而定
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().
title("前后端分离项目中使用swagger2构建restful api").
description("在springboot项目中集成swagger2").
contact("xxxxxx").
version("1.0").build();
}
}
3、controller层示例
三个主要注解:
- @Api :表示该类可以被swagger扫描到。
- @ApiOperation : 对接口的描述,有values和notes两个属性。
- @ApiImplicitParam : 对接口所接收的参数进行描述。
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
/**
* @author ljs
* @data 2020/06/09
*/
@RestController
@Api
@RequestMapping("/users")
public class UserApi {
@Autowired
UserService userService;
@ApiOperation(value = "根据id获取用户", notes = "随便写点东西")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path")
@GetMapping("/{id}")
public Response<UserDto> getUserById(@PathVariable String id) {
return Response.ok(userService.findById(id));
}
@ApiOperation(value = "根据名字获取用户", notes = "???")
@ApiImplicitParam(name = "name", value = "用户名字", required = true, paramType = "path")
@GetMapping("/{name}/search")
public Response<UserDto> getByName(@PathVariable String name) {
return Response.ok(userService.findByName(name));
}
@ApiOperation(value = "根据id删除用户", notes = "???")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path")
@DeleteMapping("/{id}")
public Response<Void> delete(@PathVariable String id) {
userService.delete(id);
return Response.ok();
}
@ApiOperation(value = "添加用户", notes = "???")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用户对象数据", required = true),
@ApiImplicitParam(name = "password", value = "用户对象数据", required = true),
}
)
@PostMapping
@ResponseBody
public Response<Void> create(@RequestBody UserInsertRequest userInsertRequest) {
userService.save(userInsertRequest);
return Response.ok();
}
@ApiOperation(value = "根据id修改用户", notes = "???")
@ApiImplicitParams({
@ApiImplicitParam(name = "userModel", value = "用户对象数据", required = true),
@ApiImplicitParam(name = "id", value = "用户id", required = true)
}
)
@PutMapping("/{id}")
public Response<Void> update(@RequestBody UserUpdataRequest userUpdataRequest, @PathVariable String id) {
userService.update(id, userUpdataRequest);
return Response.ok();
}
@ApiOperation(value = "分页测试", notes = "???")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path")
@GetMapping
public Response<Page<UserDto>> getByPage(@PageableDefault(size = 8) Pageable pageable,
UserQueryRequest userQueryRequest) {
Page<UserDto> userDtoPage = userService.findByPage(pageable, userQueryRequest);
// //userService.findUser(id);
// System.out.println(userDtoPage.getTotalElements());
return Response.ok(userDtoPage);
}
}
4、效果示例
启动项目,项目地址加/swagger-ui.html即可访问swagger提供的web端。
http://127.0.0.1:8080/swagger-ui.html
总结
swagger极大的方便了前后端分离开发的文档维护,而且swagger使用简单和提供的界面交互良好,非常棒的工具。