Java解决跨域问题

当浏览器控制台出现下面的问题,说明出现了跨域问题。

Access to XMLHttpRequest at 'http://www.float.com/goods/findPage?page=1&size=5' from origin 'www.integer.net.cn' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

此时添加以下Java代码便可以解决跨域问题:

package com.by.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class SpringmvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //放行哪些原始域
                .allowedOrigins("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域(头部信息)
                .allowedHeaders("*");
    }
}

如果出现绑定异常:

在项目的父工程中的pom.xml文件夹中添加一下代码:

<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
<build>
    <resources>
        <!-- mapper.xml文件在java目录下 -->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>

        <!--解决maven项目resources目录显示为普通目录问题-->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

 还有就是注解一定一定不要导错!!!!!!!

@Service注解的包是

import com.alibaba.dubbo.config.annotation.Service;

注入时不要用@Autowired

要使用@Reference

包不要导错:

import com.alibaba.dubbo.config.annotation.Reference;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值