【问题标题】:@CrossOrigin cannot be resolved - Spring boot@CrossOrigin 无法解决 - 春季启动
【发布时间】:2017-04-04 17:45:16
【问题描述】:

我已经使用以下 Github 存储库设置了我的 Spring Boot 工作区: https://github.com/leanstacks/spring-boot-fundamentals/tree/repository

我成功设置了 Rest API,并且还能够使用 Postman 测试所有 CRUD 操作。 现在我试图从我的 Angular 2 应用程序中调用 Rest 服务,但是当我执行 GET 操作时出现以下错误:

'http://localhost:8080/api/greetings。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:4200' 不允许访问。'

我尝试使用: @CrossOrigin(methods = RequestMethod.GET, allowCredentials = "true") 注释用于解决跨源问题,但 CrossOrigin 没有得到解决。我收到一条消息: '无法解析导入的org.springframework.web.bind.annotation.CrossOrigin'

非常感谢任何建议/反馈。

【问题讨论】:

    标签: spring angular spring-boot cross-domain


    【解决方案1】:

    我建议你确保使用最新版本的 SpringBoot(在你的 pom.xml 中):

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.4.2.RELEASE</version>
     </parent>
    

    并且一定要包含 starter-web:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        ...
    </dependencies>
    

    【讨论】:

    • 成功了!我实际上尝试过版本 -1.4.1.RELEASE,但这导致 JpaRepository 无法解析。有什么理由吗?顺便说一句,我已经有了 spring boot starter web。谢谢本75! :)
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2017-09-11
    • 2015-04-18
    • 2017-06-24
    • 2015-08-22
    相关资源
    最近更新 更多