91名师指路-头部
91名师指路

java 集成swagger

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

1 引入jar

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<swagger.version>2.7.0</swagger.version>
</properties>


<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>

2 添加配置

package com.mszl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
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;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

// 是否开启swagger,正式环境一般是需要关闭的
@Value(value = "${swagger.enabled}")
private boolean swaggerEnabled;

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.enable(swaggerEnabled)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mszl.candidate.controller"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("候选人 API")
/*.description("bobo-teacher")
.termsOfServiceUrl("teacher")*/
.version("1.0")
.build();
}




}

3 访问:http://127.0.0.1:port/swagger-ui.html




4 对应的注解。

@Api(tags="APP-候选人") 写在控制层上 如图4.1

@ApiOperation(value="新增身份卡")  写在方法上  如图4.2

@ApiModelProperty(value="手机号", required = true) 写在字段上 如图4.3


4.1



4.2 



4.3 

5 在实际中,我们在生产环境是需要关闭swagger的。我们可以通过开关去控制。


到此就完成了swagger的相关配置。


2019-08-23 14:46:59     阅读(826)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部