1

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";
    }

}
2
  • 4
    looks like you gonna need to rename your class to smth other, than Controller Commented Jul 31, 2016 at 13:51
  • 2
    Without trying it out, have you tried renaming your class? Maybe eclipse gets confused that you use your class' name as annotation? Or try the full path to the Spring-Controller class in your annotation @org.springframework....Controller Commented Jul 31, 2016 at 13:51

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When I refactored the name of my class, it also changes the name of the annotation which clearly proved that it was confusing the two. Thanks for the answer !
Glad it helps ... if so, don't forget about accepting the answer.
1

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

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

Comments

0

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

Comments

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

maybe this ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.