SpringBoot整合Swagger

Swagger用于生成、描述、調用和可視化RESTful風格的Web服務

1. Maven依賴

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

2. Swagger配置類

package com.jyh.swagger;

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;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                /** Api文檔信息配置 */
                .apiInfo(apiInfo())
                .select()
                /** 配置Api包掃描 */
                .apis(RequestHandlerSelectors.basePackage("com.jyh.swagger"))
                /** 配置路徑(PathSelectors.any()所有路徑) */
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * Api文檔信息配置
     * @return
     */
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger構建api文檔")
                .description("這里是說明")
                .version("1.0")
                .build();
    }

}

3. Swagger使用

package com.jyh.swagger;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("學生對象")
public class Student {

    @ApiModelProperty(value = "姓名", dataType = "String", required = true, notes = "填寫姓名")
    private String name;

    @ApiModelProperty(value = "年齡", dataType = "Integer", required = true, notes = "填寫年齡")
    private Integer age;

}

package com.jyh.swagger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Api:用于類
 * value:接口
 * tags:說明標簽
 */

@Api(tags = "swagger測試controller")
@RestController
@RequestMapping("/SwaggerController/")
public class SwaggerController {

    @ApiOperation(value = "addStudent", notes = "添加方法")
    @PostMapping(value = "addStudent")
    public String addStudent(@RequestBody Student student) {
        return "name:"+student.getName()+" age:"+student.getAge();
    }

}

效果圖

訪問地址:http://localhost:8080/swagger-ui.html

image.png

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容