Using @CrossOrigin in Spring Boot

10,821

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>
Share:
10,821
Jonik
Author by

Jonik

I'm a generalist software developer who's been programming professionally for 15+ years. Simplicity, code maintainability, easy deployments, automated testing, and continuous integration are some of the things close to my heart as a developer. I currently work as a freelance developer, focused on backend (Java/Scala/JVM mostly) and Android development. Enjoying Kotlin and Jetpack Compose nowadays especially! You can reach me by email through &lt;jonik at iki dot country code for finland&gt;.

Updated on June 04, 2022

Comments

  • Jonik
    Jonik almost 2 years

    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>