SpringBoot 配置 Swagger

Posted 阿 军

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 配置 Swagger相关的知识,希望对你有一定的参考价值。

1.添加 Pom 文件

            <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. 添加配置类 SwaggerConfig.java 

package com.vipsoft.web.boot.config;

import springfox.documentation.service.Contact;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.vipsoft.model.user.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());
    }

    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger")
                .description("SpringBoot整合Swagger,API接口描述......")
                .version("9.0")
                .contact(new Contact("VipSoft","www.vipsoft.com.cn","47262947@qq.com"))
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build();
    }
}

 

 

 

 

3. 添加接口说明

需要暴露出去的实体

/**
 * 用户实体
 */
@ApiModel
public class Users {
    @ApiModelProperty(value = "用户id")
    private String Id;

    @ApiModelProperty(value = "用户名")
    private String userName;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "真实姓名")
    private String realName;

    private Date createTime;
。。。。。
}

Controller

@Api(description = "用户操作接口")
@RestController
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);
    Logger loggerApiHis = LoggerFactory.getLogger("apiHis"); //apiHis 对应 logback.xml中的配置


    /**
     * Mybatis 获取 List 列表
     *
     * @param request
     * @return
     */

    @ApiOperation(value = "获取用户信息", notes = "这边可以不写")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主健", defaultValue = "深圳", required = true)
    })
    @GetMapping(value = "/get/{id}")
    public Users get(HttpServletRequest request, @PathVariable("id") String id) {
        return userService.get(id);
    }

 

 

 启动类上加@EnableSwagger2 否则会报 Swagger2 Unable to infer base url. This is common (降低版本好像也可以没试过)

 

 


 

输入访问地址:http://localhost:8080/swagger-ui.htm

 

以上是关于SpringBoot 配置 Swagger的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot——SpringBoot集成Swagger生成API文档

SpringBoot——SpringBoot集成Swagger生成API文档

springboot之swagger快速启动

SpringBoot使用拦截器和swagger(knife4j)配置

SpringBoot使用拦截器和swagger(knife4j)配置

SpringBoot框架 之 Swagger2