码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 配置 Swagger

时间:2020-06-20 23:49:08      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:mod   详细   oca   api接口   stc   swa   let   style   package   

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

标签:mod   详细   oca   api接口   stc   swa   let   style   package   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!