L01c
L01c

Reputation: 1063

@Controller is not an annotation

I am trying to create a new project with SpringBoot, the @Controller annotation seems to work but Eclipse is complaining saying it is not an annotation. Any ideas to resolve that ?

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>gpID</groupId>
    <artifactId>myApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>My App</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-youtube</artifactId>
            <version>v3-rev177-1.22.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Controller.java

package myApp.controllers;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller // This line is not understood in Eclipse
@EnableAutoConfiguration
public class Controller {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

}

Upvotes: 1

Views: 5709

Answers (4)

Saran Raj
Saran Raj

Reputation: 1

if you want the name of the file to be "controller", you have to put annotation fully qualified annotation. otherwise it will work when you add @controller

Upvotes: 0

GhostCat
GhostCat

Reputation: 140417

Your code is confusing, but lets be precise:

  1. In the end, "custom" annotations are java classes. So, like with any other class - when you want to use them in your source code, then you need an import statement for that very class. So, in your case: figure the "full" name for the controller annotation you intend to use, make sure that its class file is in your build path, and then add the import statement. Like with: import org.springframework.stereotype.Controller;
  2. Do not call your class "X" when you intend to user another annotation called X, too

Point 1 should fix your "compile" problem; and point 2 is really essential if you don't want to confuse yourself and other future readers of your code. Yes, java allows for having x.Y and z.Y class names; but if possible: avoid it! You see, when you are using the name Y in two different meanings within one class, then for example, you have to use absolute names like x.Y and z.Y all the time.

Upvotes: 4

Daniel Figueroa
Daniel Figueroa

Reputation: 10666

You need to use the fully qualified name for the annotation since your class is named Controller

@org.springframework.stereotype.Controller
public class Controller {...

Upvotes: 1

O.Kuz
O.Kuz

Reputation: 387

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.0.RELEASE</version>
</dependency>

maybe this ?

Upvotes: -1

Related Questions