解决办法:添加一个配置类,实现WebMvcConfigurer接口中的addCorsMappings方法
代码如下:
package com.cxy.mybatisplus.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
//表面这是一个配置类,将会被IOC容器管理
public class CrosConfig implements WebMvcConfigurer{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/*")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("PUT","GET","POST","DELETE","PATCH")
.maxAge(3600);
}
}