SwaggerConfig:
@Configuration
@EnableSwagger2
@Profile({"dev", "test", "local"})
public class Swagger2Config extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.tencent.iov.open.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
String desc = "公共header参数说明(需URLEncoder编码):\r\n"
+ "参数名:Version-Code \r\t 参数类型: String \r\t 参数说明:版本号 \r\n "
+ "参数名:Platform \r\t 参数类型: String \r\t 参数说明:用于标识平台类型:Android、iOS \r\n "
+ "参数名:Device-Id \r\t 参数类型: String\r\t 参数说明:设备唯一标识 \r\n "
+ "参数名:Device-Type \r\t 参数类型: String \r\t 参数说明:设备型号 \r\n "
+ "参数名:Timestamp \r\t 参数类型: Long \r\t 参数说明:毫秒时间戳 \r\n "
+ "Cookie 参数说明:\r\n"
+ "参数名:token \r\t 参数类型: String \r\t 参数说明:令牌 \r\n ";
return new ApiInfoBuilder().title("开发平台接口文档").description(desc).termsOfServiceUrl("")
.contact(new Contact("panqian", "", "18682387045@qq.com")).version("develop_1.5").build();
}
/**
* swagger ui资源映射
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger-ui.html路径映射,浏览器中使用/api-docs访问
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs", "/swagger-ui.html");
}
}
跨域配置文件 CorsConfig:
@Configuration
@EnableWebMvc
public class CorsConfig extends WebMvcConfigurationSupport {
/*
* 这里主要为了解决跨域问题,所以重写addCorsMappings方法
*/
@Override
protected void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.exposedHeaders("access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-max-age",
"X-Frame-Options")
.allowCredentials(true).maxAge(3600);
super.addCorsMappings(registry);
}
/**
* 跨域配置后swagger2可能不能访问,需要增加如下配置
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/71387.html