當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Swagger2簡介
1、Swagger2優點
整合到Spring Boot中,構建強大RESTful API文檔。省去接口文檔管理工作,修改代碼,自動更新,Swagger2也提供了強大的頁面測試功能來調試RESTful API。
2、Swagger2常用注解
Api:修飾整個類,描述Controller的作用 ApiOperation:描述一個類的一個方法,或者說一個接口 ApiParam:單個參數描述 ApiModel:用對象來接收參數 ApiProperty:用對象接收參數時,描述對象的一個字段 ApiResponse:HTTP響應其中1個描述 ApiResponses:HTTP響應整體描述 ApiIgnore:使用該注解忽略這個API ApiError :發生錯誤返回的信息 ApiImplicitParam:一個請求參數 ApiImplicitParams:多個請求參數二、與SpringBoot2.0 整合
1、核心依賴
spring-boot:2.1.3.RELEASE swagger:2.6.12、Swagger2 配置
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; /*** Swagger 配置文件*/ @Configuration public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.swagger.two")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SpringBoot利用Swagger構建API文檔").description("使用RestFul風格, 創建人:知了一笑").termsOfServiceUrl("https://github.com/cicadasmile").version("version 1.0").build();} }3、啟動類添加注解
@EnableSwagger2 @SpringBootApplication public class SwaggerApplication {public static void main(String[] args) {SpringApplication.run(SwaggerApplication.class,args) ;} }4、啟動效果圖
三、增刪改查案例
1、添加用戶
(1)、代碼塊
@ApiOperation(value="添加用戶", notes="創建新用戶") @ApiImplicitParam(name = "user", value = "用戶詳細實體user", required = true, dataType = "User") @RequestMapping(value = "/addUser", method = RequestMethod.POST) public ResponseEntity<JsonResult> addUser (@RequestBody User user){JsonResult result = new JsonResult();try {users.put(user.getId(), user);result.setResult(user.getId());result.setStatus("ok");} catch (Exception e) {result.setResult("服務異常");result.setStatus("500");e.printStackTrace();}return ResponseEntity.ok(result); }(2)、效果圖
2、用戶列表
(1)、代碼塊
@ApiOperation(value="用戶列表", notes="查詢用戶列表") @RequestMapping(value = "/getUserList", method = RequestMethod.GET) public ResponseEntity<JsonResult> getUserList (){JsonResult result = new JsonResult();try {List<User> userList = new ArrayList<>(users.values());result.setResult(userList);result.setStatus("200");} catch (Exception e) {result.setResult("服務異常");result.setStatus("500");e.printStackTrace();}return ResponseEntity.ok(result); }(2)、效果圖
3、用戶查詢
(1)、代碼塊
@ApiOperation(value="用戶查詢", notes="根據ID查詢用戶") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer", paramType = "path") @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET) public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){JsonResult result = new JsonResult();try {User user = users.get(id);result.setResult(user);result.setStatus("200");} catch (Exception e) {result.setResult("服務異常");result.setStatus("500");e.printStackTrace();}return ResponseEntity.ok(result); }(2)、效果圖
4、更新用戶
(1)、代碼塊
@ApiOperation(value="更新用戶", notes="根據Id更新用戶信息") @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long",paramType = "path"),@ApiImplicitParam(name = "user", value = "用戶對象user", required = true, dataType = "User") }) @RequestMapping(value = "/updateById/{id}", method = RequestMethod.PUT) public ResponseEntity<JsonResult> updateById (@PathVariable("id") Integer id, @RequestBody User user){JsonResult result = new JsonResult();try {User user1 = users.get(id);user1.setUsername(user.getUsername());user1.setAge(user.getAge());users.put(id, user1);result.setResult(user1);result.setStatus("ok");} catch (Exception e) {result.setResult("服務異常");result.setStatus("500");e.printStackTrace();}return ResponseEntity.ok(result); }(2)、效果圖
5、刪除用戶
(1)、代碼塊
@ApiOperation(value="刪除用戶", notes="根據id刪除指定用戶") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long", paramType = "path") @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE) public ResponseEntity<JsonResult> deleteById (@PathVariable(value = "id") Integer id){JsonResult result = new JsonResult();try {users.remove(id);result.setResult(id);result.setStatus("ok");} catch (Exception e) {result.setResult("服務異常");result.setStatus("500");e.printStackTrace();}return ResponseEntity.ok(result); }(2)、效果圖
四、源代碼
GitHub:知了一笑 https://github.com/cicadasmile/middle-ware-parent總結
以上是生活随笔為你收集整理的SpringBoot2.0 整合 Swagger2 ,构建接口管理界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 易宝典文章——用ISA 2006标准版发
- 下一篇: Q-learning家族【强化学习】