Jonik
Jonik

Reputation: 81771

Using @CrossOrigin in Spring Boot

I'm using latest Spring Boot (1.2.7.RELEASE). I would like to use the @CrossOrigin annotation from the package org.springframework.web.bind.annotation as described in CORS Support section in Spring docs.

I'd think I already have all the necessary dependencies (via Spring Boot defaults), but this is confusing: CrossOrigin is not found, even though stuff like RestController from the same package works!

enter image description here

Error:(8, 47) java: cannot find symbol
  symbol:   class CrossOrigin
  location: package org.springframework.web.bind.annotation

What's going on? Has CrossOrigin been removed from later versions of Spring-MVC, or am I missing some dependency?

pom.xml:

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

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

Upvotes: 3

Views: 14663

Answers (2)

Venkat IndianEagle
Venkat IndianEagle

Reputation: 159

@CrossOrigin annotation is used to provide support for cross domains,

ie nothing but from different domains also we can access that service

by default it is true.

we can access the services from cross domains also.

Upvotes: 0

Jonik
Jonik

Reputation: 81771

Alright, looks like the latest Spring Boot release, 1.2.7.RELEASE at the moment, is too old to have a version of Spring MVC with CrossOrigin. (Spring Boot 1.2.7 uses Spring version 4.1.8).

I updated to latest Spring Boot 1.3 release candidate (1.3.0.RC1) and it works:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RC1</version>
</parent>

Also needed to specify spring-milestones repository in pom.xml to be able to use the non-release version.

<repositories>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </repository>
</repositories>

Update: override Spring version

As Stéphane Nicoll pointed out, a simpler way to get Spring 4.2.2 classes (such as CrossOrigin) into use is this:

<properties> 
    <!-- ... -->
    <spring.version>4.2.2.RELEASE</spring.version>
</properties>

Upvotes: 6

Related Questions