接口跨域解决方法

/**
* 全局设置
*
*/
@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;
    }
}

 

posted @ 2019-07-25 21:02  npe0  阅读(686)  评论(0编辑  收藏  举报