0

Учусь создавать web app. На офф.сайте spring нашел зависимости и попытался создать контроллер

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Controller {
    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("main", "Main Page");
        model.addAttribute("world", "world");
        return "main";
    }
}

подсвечивает как ошибку в классе Controller запись с импортам

import org.springframework.stereotype.Controller;

Вот ошибка при выполнении

COMPILATION ERROR : 
-------------------------------------------------------------
webapp/controllers/Controller.java:[3,1] webapp.controllers.Controller is already defined in this compilation unit
webapp/controllers/Controller.java:[7,2] incompatible types: webapp.controllers.Controller cannot be converted to java.lang.annotation.Annotation
2 errors 
-------------------------------------------------------------
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  8.475 s
Finished at: 2020-06-16T22:33:27+03:00
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project WebApp: Compilation failure: Compilation failure: 
webapp/controllers/Controller.java:[3,1] webapp.controllers.Controller is already defined in this compilation unit
webapp/controllers/Controller.java:[7,2] incompatible types: webapp.controllers.Controller cannot be converted to java.lang.annotation.Annotation
-> [Help 1]

Сам 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>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!--  lookup parent from repository  -->
</parent>
    
<groupId>com.elvpr</groupId>
<artifactId>WebApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>WebApp</name>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>        
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <jakartaee>8.0</jakartaee>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

 <build>                  
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-api</artifactId>
                                <version>${jakartaee}</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

скажите может какой зависимости не хватает?

0

1 ответ 1

2

Вы назвали класс одинаково с интерфейсом из Spring:

import org.springframework.stereotype.Controller;

Из за чего Java не может достать реализацию Spring, потому что постоянно натыкается на ваш класс:

webapp/controllers/Controller.java:[3,1] webapp.controllers.Controller is already defined in this compilation unit

Чтобы исправить эту проблему, просто переименуйте название вашего класса.

Вообще, называть класс контроллера Controller плохая практика, лучше указывать область видимости для названия данного класса, например: HomeController или MainController.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.