【问题标题】:How to reference an Eclipse locally deployed spring app from Angular?如何从 Angular 引用 Eclipse 本地部署的 spring 应用程序?
【发布时间】:2021-12-23 16:26:48
【问题描述】:

有一个Spring 项目部署在Eclipse 本地服务器上(基于Tomcat)。

我想从Angular 引用这个项目。我试过 http://localhost:8083/gmao-0.0.1-SNAPSHOT/gmao/ ,然后是 http://localhost:8083/gmao/ 但两者都不起作用。

那么如何引用Spring项目呢?

【问题讨论】:

  • 在什么情况下不起作用?甚至在 CLI 中使用 curl?只是来自Angular?如果是这样,您的浏览器内工具会显示连接失败消息吗?

标签: angular spring eclipse tomcat


【解决方案1】:

网站只能通过所谓的同源策略访问来自同源的资源。因此,您需要启用CORS [跨域资源共享],以便连接运行在不同端口上的 FrontEnd(Angular) 和 BackEnd(Java)。

在春天,你可以做这样的事情

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
        .allowedOrigins("http://localhost:4200");
    }
}

这将启用从您的 Angular 应用程序(在端口 4200 上运行)到 Spring 应用程序中的任何端点的 CORS 请求。

另一种简单的方法是在Controller/Class级别或Controller中的API方法级别添加@CrossOrigin

@CrossOrigin(origins = "http://localhost:4200")

我希望这能回答你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多