原
java 集成swagger
1 引入jar
UTF-8
1.8
1.8
2.7.0
io.springfox
springfox-swagger2
${swagger.version}
io.springfox
springfox-swagger-ui
${swagger.version}
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="手机号") 写在字段上 如图4.3
4.1
4.2
4.3
5 在实际中,我们在生产环境是需要关闭swagger的。我们可以通过开关去控制。
到此就完成了swagger的相关配置。