总结
两个java文件,CROSConfig(解决跨域)与SwaggerConfig(配置Swagger2),按需创建文件

跨域
跨域后端
直接复制最好,省得报错
CROSConfig文件中
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author shuyang
* @Description:
* @create 2020-04-20 16:03
*/
@Configuration
public class CROSConfig extends WebMvcConfigurationSupport {
//解决swagger2无法访问问题,不用swagger2就不写这个方法
//在SpringBoot没有直接看到META-INF这个文件夹,以为没用
//结果发现是有用的,直接复制就完事了
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
//跨域配置,可直接复制
//有时候前端sessionId会重复,其实是前端没有配置好
//只要postman测试通过了,前端却出问题,那就是前端少配置
@Override
public void addCorsMappings(CorsRegistry registry) {
//听说 allowedOrigins 里写 * 会导致取不到cookies
//我还没试过,也不知道怎么解决,希望有人能给出解决方案吧
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
.allowCredentials(true).maxAge(3600);
}
}
跨域前端
其实我不会前端配置跨域,所以就遇到了同一个浏览器同一页面访问接口,出现sessionsId重复的问题,找了很久,发现还是前端少了配置
这时候,在axios里配置这个
axios.defaults.withCredentials = true
完美解决,舒坦了
Swagger2配置
SwaggerConfig文件中
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 shuyang
* @Description:
* @create 2020-05-11 13:01
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig{
//都是配置信息,需要改的只有basePackage,改成需要扫描接口的包名
//建议直接写com.xxx,写多了反而容易错
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.teabackend"))//改成自己的包名
.paths(PathSelectors.any())
.build();
}
//细心的朋友肯定发现了,其实我的swagger也是百度出来的,不过确实能用
// " " 双引号内的都可以改,都是显示信息而已,比如那个网址,类似于联系地址,不会影响配置
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot Swagger 测试")
.description("Springboot 整合Swagger2")
.termsOfServiceUrl("localhost:8080/ruleengine")
.contact(new Contact("MyName", "https://localhost", "email.com"))
.version("1.0")
.build();
}
}
swagger2的依赖,直接复制
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
有的博客里写的swagger2访问地址都是稀奇古怪的,还加了一大堆前缀后缀的,经过测试与百度查询后发现。
直接访问http://localhost:8080/swagger-ui.html就可以看到接口了,按照我上面的配置,就是这个地址。
建议查看下方操作手册
Swagger2操作手册
2020-5-12创建

221

被折叠的 条评论
为什么被折叠?



