SpringBoot整合Swagger

本文主要是总体配置和简单示例。

具体每一个注解的用法,需要的再自行查找。

 

Maven配置

<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>
		<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
		<dependency>
			<groupId>io.swagger.core.v3</groupId>
			<artifactId>swagger-annotations</artifactId>
			<version>2.0.8</version>
		</dependency>

		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>swagger-bootstrap-ui</artifactId>
			<version>1.8.8</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>28.0-jre</version>
		</dependency>

 

Swagger Config配置

package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2配置
 * http://localhost:18005/swagger-ui.html
 * http://localhost:18005/doc.html
 */
@Configuration
@EnableSwagger2
@Profile({"dev", "pre"})
class SwaggerConfig {
    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //为当前包路径
                //.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Swagger2构建RESTful API文档,Demo项目")
                //创建人
                .contact(new Contact("雷哥", "http://leige.cn", "leige@111.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

Controller示例代码

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.bean.model.User;
import com.demo.common.result.Result;
import com.demo.common.result.ResultUtils;
import com.demo.controller.BaseController;
import com.demo.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("api/user")
@Api("UserController相关的api")
public class UserController extends BaseController {

	@Resource
	private UserService userService;

	@ApiOperation(value = "查询用户", notes = "获得用户信息")
	//@RequestMapping(value = "/user", method = {RequestMethod.POST,RequestMethod.GET})
	@PostMapping("/getUser")
	//@ApiImplicitParam(name = "user", dataType = "User", value = "用户实体类", paramType = "query", required = true)
	private Result<User> getUser(User user) {
		User userResult =  new User();
		userResult.setName("雷哥");
		userResult.setEmail("fansunion@qq.com");
		return ResultUtils.succeed(userResult);
	}

}

Model模型类

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel("用户实体类")
@Data
public class User {

	@ApiModelProperty(name="name",value="用户名",required=true)
	private String name;

	@ApiModelProperty(name="email",value="邮箱",required=false)
	private String email;
	
	
}

 

访问效果

 

 

 

如果项目中用了Shiro实现权限控制(虽然本人不喜欢用这玩意)

放过对Swagger2的拦截

    //对swagger2相关接口放行,5个配置
        filterChainDefinitionMap.put("/doc.html", "anon");
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/v2/api-docs/**", "anon");
        filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");

 

http://localhost:18004/swagger-ui.html   Swagger官方UI

http://localhost:18004//doc.html   第三方Bootstarp皮肤的UI

 

 

参考资料

swagger2 注解说明 ( @ApiImplicitParams )

https://blog.csdn.net/jiangyu1013/article/details/83107255

 

spring boot项目中使用swagger2

https://www.jianshu.com/p/05be40b9a7a3

 

spring boot 整合 swagger2,并设置post,get请求方式

https://blog.csdn.net/qq_36249132/article/details/90109815

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值