首页 > 编程语言 > 详细

SpringBoot 配置 Swagger

时间:2020-06-20 23:05:12      阅读:109      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/vipsoft/p/13170766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!