简体   繁体   English

Controller Class 从未在 Springboot 中使用 Maven

[英]Controller Class Never Used in Springboot Maven

I have created a very basic Spring Boot rest api Application.我创建了一个非常基本的 Spring Boot rest api 应用程序。 But somehow the application is not able to read the controller class, I am Using IntelliJ Idea Community Version.但不知何故,应用程序无法读取 controller class,我使用的是 IntelliJ Idea 社区版。

If I hover the mouse to the controller class name it says - the class is never used.如果我 hover 鼠标到 controller class 它说的名字 - class 从未使用过。 The application starts with no error, Tomcat server is up and running.应用程序启动没有错误,Tomcat 服务器已启动并正在运行。

But while accessing the http://localhost:8080/details url I am getting 404 not found.但是在访问 http://localhost:8080/details url 时,我收到 404 not found。

I have tried scanbasepackages too.我也尝试过 scanbasepackages。 The Controller class is in subpackage. Controller class 在分包中。

Below is my POM file:下面是我的 POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.5.0</version>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>

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

</project>

Controller class: Controller class:

package com.example.demo.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class APIController {
    @GetMapping("/details")
    public String foo() {
        return "success";
    }
}

Application class:申请class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(scanBasePackages={"com.example.demo"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

If anyone can help please, Thanks in advance.如果有人可以提供帮助,请提前致谢。

Add this dependency to your pom.xml将此依赖项添加到您的 pom.xml

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

You can use start.spring.io and add missing dependencies.您可以使用start.spring.io并添加缺少的依赖项。 In your case you need spring-boot-starter-web在您的情况下,您需要spring-boot-starter-web

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

You can click on button explore and you will see a pom.xml file and simply add missing dependency to your project.您可以单击按钮探索,您将看到一个 pom.xml 文件,只需将缺少的依赖项添加到您的项目中即可。

After that you can reload pom.xml file to resolve new maven dependencies.之后你可以重新加载 pom.xml 文件来解决新的 maven 依赖。

You can use this approach whenever you need to add a new dependency to your spring boot project.只要您需要向 spring 启动项目添加新的依赖项,就可以使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM