1. 程式人生 > >springBoot(15):集成Swagger

springBoot(15):集成Swagger

springboot 集成swagger

一、簡介

Swagger 是一個規範和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。http://swagger.io/

Springfox 的前身是swagger-springmvc,是一個開源的API doc框架,可以將我們的 Controller的方法以文檔的形式展現,基於Swagger。http://springfox.github.io/springfox/

二、操作

2.1、添加依賴

<!-- Swagger -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.6.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.6.0</version>
</dependency>

2.2、代碼

package com.example.demo.utils.configuration;

import static springfox.documentation.builders.PathSelectors.regex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置
 * @Author: 我愛大金子
 * @Description: Swagger配置
 * @Date: Create in 11:33 2017/6/22
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    @Bean
    public Docket accessToken() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("index")// 定義組
                .select() // 選擇那些路徑和 api 會生成 document
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) // 攔截的包路徑
                .paths(regex("/index/.*"))// 攔截的接口路徑
                .build() // 創建
                .apiInfo(apiInfo()); // 配置說明
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()//
                .title("測試")// 標題
                .description("spring boot 全集")// 描述
                .termsOfServiceUrl("http://www.roncoo.com")//
                .contact(new Contact("我愛大金子", "http://1754966750.blog.51cto.com/", "[email protected]
/* */"))// 聯系 // .license("Apache License Version 2.0")// 開源協議 // .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")// 地址 .version("1.0")// 版本 .build(); } }


測試用的Controller:

@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private UserMapper mapper;

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String, Object> index(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = mapper.selectByPrimaryKey(id);
        if (null != user) {
            map.put("id", user.getId());
            map.put("name", user.getName());
        } else {
            map.put("id", -1);
            map.put("name", "error");
        }
        return map;
    }
}


效果:訪問http://localhost:8989/swagger-ui.html

技術分享


技術分享

2.3、自定義(註解的使用)

@ApiIgnore

忽略暴露的 api

@ApiOperation(value = "查找", notes = "根據用戶 ID 查找用戶")

添加說明


其他註解:

@Api :用在類上,說明該類的作用

@ApiImplicitParams :用在方法上包含一組參數說明

@ApiResponses :用於表示一組響應

@ApiResponse :[email protected] 中,一般用於表達一個錯誤的響應信息

code:數字,例如 400

message:信息,例如"請求參數沒填好"

response:拋出異常的類

@ApiModel :描述一個 Model 的信息(這種一般用在 post 創建的時候,[email protected] 這樣的場景,[email protected] 註解進行描述的時候)

@ApiModelProperty :描述一個 model 的屬性


更多請查看:https://github.com/swagger-api/swagger-core/wiki/Annotations

本文出自 “我愛大金子” 博客,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1940912

springBoot(15):集成Swagger