@CrossOrigin 无法解析 - Spring 启动

@CrossOrigin cannot be resolved - Spring boot

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

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

'http://localhost:8080/api/greetings. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200'因此不允许访问。'

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

非常感谢suggestion/feedback。

我建议您确保使用最新版本的 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>