接口跨域解决方法
/** * 全局设置 * */ @Configuration public class CustomCorsConfiguration2 extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**").allowedOrigins("http://localhost:8080"); } } /** *细粒度配置 * */ @RestController @RequestMapping(value = "/api", method = RequestMethod.POST) public class ApiController { @CrossOrigin(origins = "http://localhost:8080") @RequestMapping(value = "/get") public HashMap<String, Object> get(@RequestParam String name) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("msg", "跨域测试"); map.put("name", name); return map; } }
作者:null-npe 出处:https://www.cnblogs.com/npe 由于作者个人水平有限,如果文中有什么错误,欢迎指出。以免更多的人被误导。 |