swagger在springBoot中的使用记录

参考 http://blog.didispace.com/springbootswagger2/

 springfox-swagger原理解析与使用过程中遇到的坑_w4hechuan2009的博客-CSDN博客 

http://blog.csdn.net/cool__wang/article/details/49466609

由于要使用swagger来进行前后端交互,所以在百度上学习,另外也简单记录下,免得忘记

demo是基于  Spring Tool Suite 插件的向导初始化的springBoot项目,感觉先建立个maven项目然后导入手动依赖应该也是可以的

首先是springBoot的依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.7.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

swagger的依赖

			<!--swagger -->
			<dependency>
				<groupId>io.springfox</groupId>
				<artifactId>springfox-swagger2</artifactId>
				<version>2.5.0</version>
			</dependency>
			<dependency>
				<groupId>io.springfox</groupId>
				<artifactId>springfox-swagger-ui</artifactId>
				<version>2.5.0</version>
			</dependency>
			<!-- swagger-ui 风格2 -->
			<!-- <dependency>
				<groupId>io.springfox</groupId>
				<artifactId>springfox-swagger-ui</artifactId>
				<version>2.2.2</version>
			</dependency> -->
		</dependencies>


第二种风格是在遇到一个问题的时候找到的ui,效果看起来不错,所以顺手放在上面了

配置类

package com.example.auth.app.base;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author liu 2017-10-28 13:11
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
	@Bean
	public Docket authRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("auth").apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.example.auth.app.facade")).paths(PathSelectors.any())
				.build();
	}

	@Bean
	public Docket controllerRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("org").apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.example.auth.app.controller")).paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("RESTful APIs")
				// .description("这是一段描述")
				// .termsOfServiceUrl("http://www.baidu.com/")
				// .contact(new Contact("张三","邮箱地址@qq.com","xx楼xx号"))
				.version("1.0").build();
	}
}


至此基本的构建已经完成

localhost:8080/swagger-ui.html  可以看到结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值