swagger版本与UI版本可以不一样(自己笨才想到)
io.springfox
springfox-swagger2
2.8.0
io.springfox
springfox-swagger-ui
2.8.0
注意:我以前用的jar是2.5.0用这种方法不行,换到2.8.0就好了
.apis(RequestHandlerSelectors.basePackage("***.dome."))也可以多包扫描
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("***.dome."))//这里是controller所处的包路径
.paths(PathSelectors.any())
.build();
}
//构建api文档的详细信息函数
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("页面标题")
//描述
.description("api查询测试接口")
.termsOfServiceUrl("API terms of service")
.licenseUrl("http//localhost:8080/")
.version("1.0")//版本号s
.build();
}
}
第二种
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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 {
// 定义分隔符
private static final String splitor = ";";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(basePackage("**.controller"+splitor+"**.controller2"))//这里是controller所处的多包名
.paths(PathSelectors.any())
.build();
}
//构建api文档的详细信息函数
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("页面标题")
//描述
.description("api查询测试接口")
.termsOfServiceUrl("API terms of service")
.licenseUrl("http//localhost:8080/")
.version("1.0")//版本号s
.build();
}
public static Predicate basePackage(final String basePackage) {
return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
}
private static Function, Boolean> handlerPackage(final String basePackage) {
return input -> {
// 循环判断匹配
for (String strPackage : basePackage.split(splitor)) {
boolean isMatch = input.getPackage().getName().startsWith(strPackage);
if (isMatch) {
return true;
}
}
return false;
};
}
private static Optional extends Class>> declaringClass(RequestHandler input) {
return Optional.fromNullable(input.declaringClass());
}
}
}
@Api(tags = {"接口描述"},description = "详细描述")
@RestController
@RequestMapping("/test")
public class TestController{
//返回参数描述--注解
@ApiResponses({@ApiResponse(code = 1,message = "成功"),@ApiResponse(code = 2,message = "error失败")})
//接口详情描述--注解
@ApiOperation(value = "接口详情",notes = "接口详情")
@GetMapping("/dome")
//请求参数描述--注解
@ApiImplicitParams({@ApiImplicitParam(name = "code",value = "编号",required = true)})
public JSONObject getOrderInfoByOrderCode(@RequestParam("code") String code){
JSONObject json = new JSONObject();
return json;
}
}
网上查询很多文章没有测试通过,只有用这个笨方法了,在.licenseUrl("http//本机IP地址:8080/") 写死本机地址,生产还地址了就无妨问swagger-ui.thml 启动不同端口与不一样的服务器地址,我自己没有测试通过别的配置方法来限制。大家有好的方法谢谢留言
-dev.yml -test.yml prod.yml 配置文件信息