SpringBoot 整合Swagger2

2019-04-16  本文已影响0人  久伴我还是酒伴我

简介

现在大家开发基本用的都是Restful风格,而它的搭档Swagger也就不用多说了,不仅能帮我们在线快速生成接口文档,还能进行接口功能测试,本片文章总结下怎么能快速将Swagger2整合到自己的项目中,话不多说,一个字:

Maven Pom.xml 引入

 <!-- 与swagger一起使用,需要注意-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

Swagger 配置文件

package com.glj.member.produce.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
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 {

    @Value("${swagger2.enable}")
    private boolean enable;//是否开启swaager,如果生产环境,则禁止

    //如果不需要进行模块区分,直接用默认模块即可
    @Bean("默认模块")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("默认模块")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.glj.member.produce"))//Swagger Api扫描包路径
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("用户模块")
    public Docket createUserApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户模块")
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.glj.member.produce"))//Swagger Api扫描包路径
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/user.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(enable);//是否开启
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot 整合Swagger")//Ui页面Title
                .description("KXL oauth2-sso-client-member-produce  API") //说明
                .termsOfServiceUrl("https://www.jianshu.com/nb/35744583")//自己或者单位的官方服务地址
                .version("2.0")//版本
                .build();
    }

}

Controller 类

package com.glj.member.produce.oauth2.controller;

import com.glj.member.produce.oauth2.service.ISysUserService;
import com.glj.model.entity.SysUserPo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author gaoleijie
 * @Date 2019/4/15 20:08
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @PostMapping(value = "/getUserByUserCode", consumes = MediaType.APPLICATION_JSON_VALUE)
    public SysUserPo getUserByUserCode(@ApiParam("userCode") String userCode) {
        return userService.getUserByUserCode(userCode);
    }
}

效果图

效果图

结束语

就这么简单,这些都是自己经常使用的,也够用,也没有太深研究,如果总结不到位或者漏掉的部分,欢迎评论指正。

上一篇下一篇

猜你喜欢

热点阅读