簡(jiǎn)介
現(xiàn)在大家開發(fā)基本用的都是Restful風(fēng)格玛臂,而它的搭檔Swagger也就不用多說了唾那,不僅能幫我們?cè)诰€快速生成接口文檔,還能進(jìn)行接口功能測(cè)試拔恰,本片文章總結(jié)下怎么能快速將Swagger2整合到自己的項(xiàng)目中,話不多說基括,一個(gè)字:干颜懊。
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,如果生產(chǎn)環(huán)境风皿,則禁止
//如果不需要進(jìn)行模塊區(qū)分河爹,直接用默認(rèn)模塊即可
@Bean("默認(rèn)模塊")
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("默認(rèn)模塊")
.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("http://www.reibang.com/nb/35744583")//自己或者單位的官方服務(wù)地址
.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);
}
}
效果圖
效果圖
結(jié)束語
就這么簡(jiǎn)單,這些都是自己經(jīng)常使用的桐款,也夠用咸这,也沒有太深研究,如果總結(jié)不到位或者漏掉的部分魔眨,歡迎評(píng)論指正媳维。