首页 > 编程语言 > 详细

Springboot 集成Swagger

时间:2020-01-09 20:25:52      阅读:118      评论:0      收藏:0      [点我收藏+]

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>

  

Swagger配置类:

package com.lsr.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Description : 配置类
 * @program : demo
 * @Package : com.lsr.demo.swagger
 * @Author : Hacker_lsr@126.com
 * @Version : 1.0
 */
@Configuration
@EnableSwagger2
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {
    @Value("${swagger.enable}")
    private boolean enable;
    @Value("${swagger.controller}")
    private String controller;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.license}")
    private String license;
    @Value("${swagger.licenseUrl}")
    private String licenseUrl;
    @Bean
    public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .enable(enable)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(controller))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title(title)
                            .description(description)
                            .version(version)
                            .license(license)
                            .licenseUrl(licenseUrl)
                            .build());
        }

}  

application.properties

# swagger配置
#开关 swagger.enable=false
#扫描controller 类包路径 swagger.controller=com.lsr.controller
#标题 swagger.title=Terry Api 测试
#描述 swagger.description=开始测试吧........
#版本 swagger.version=9.2
#外连接 swagger.license=博客
swagger.licenseUrl=https://www.cnblogs.com/hacker-lsr/

  

测试Controller(注解的使用)

package com.lsr.controller;

import com.lsr.entity.BaseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.management.Agent;

/**
 * @Description : 测试丝袜哥
 * @program : demo
 * @Package : com.lsr.controller
 * @Author : Hacker_lsr@126.com
 * @Version : 1.0
 */
@RestController
@Api(tags = "测试丝袜哥")
@RequestMapping("/test")
public class TestController {
    @PostMapping("/add")
    @ApiOperation("添加用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name ="name",value = "名字",defaultValue = "测试")})

    public BaseEntity insert(String name ,String age){
        return  new BaseEntity();
    }
}

SwaggerUI

技术分享图片

 

点击 try it out

技术分享图片

 

 输入参数执行Excute

技术分享图片

 

 成功:

技术分享图片

Springboot 集成Swagger

原文:https://www.cnblogs.com/hacker-lsr/p/12172950.html

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