【发布时间】:2020-05-03 15:09:25
【问题描述】:
使用 React 的 Spring Boot
从源“http://localhost:3000”访问“http://localhost:8080/”处的 XMLHttpRequest 已被 CORS 策略阻止:
这是一个返回所有区域对象的控制器
从源“http://localhost:3000”访问位于“http://localhost:8080/”的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
package com.ministry.demo.controller;
import com.ministry.demo.model.District;
import com.ministry.demo.repository.DistrictRepository;
import com.ministry.demo.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(path = "district")
public class DistrictController {
@Autowired
DistrictService service;
@GetMapping(path = "getAll")
List<District> getAllDistrict(){
return service.getAllDistricts();
}
}
【问题讨论】:
标签: reactjs spring-boot controller cors-anywhere