美文网首页
SpringBoot整合Swagger

SpringBoot整合Swagger

作者: jyhnp | 来源:发表于2020-10-03 10:28 被阅读0次

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

相关文章

网友评论

      本文标题:SpringBoot整合Swagger

      本文链接:https://www.haomeiwen.com/subject/azqwuktx.html