繁体   English   中英

Sping Boot 基本项目未在 Intellij 社区版中运行相同的代码在 STS 中运行

[英]Sping Boot basic project not running in Intellij Community Edition Same code runs in STS

我在 IntelliJ 社区版中创建了一个包含以下三个文件的基本 Spring 项目。

启动器 Class:

package com.learn.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StarterClass {

    public static void main(String[] args) {
        SpringApplication.run(StarterClass.class,args);
    }
}

你好。java

package com.learning.spring.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello {

    @RequestMapping("/hello")
    public String hi() {
        return "Hello How Are you doing in IntelliJ";
    }
}

pom.xml:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringProjectIntelliJ28082022</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.14</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

当我运行应用程序时,我在控制台中得到以下信息:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.5.14)

2022-08-28 15:07:36.980  INFO 9408 --- [           main] com.learn.spring.StarterClass            : Starting StarterClass using Java 1.8.0_291 on ******** with PID 9408 (S:\Java_Workspaces\spring\SpringProjectIntelliJ28082022\target\classes started by **** in S:\Java_Workspaces\spring\SpringProjectIntelliJ28082022)
2022-08-28 15:07:36.981  INFO 9408 --- [           main] com.learn.spring.StarterClass            : No active profile set, falling back to 1 default profile: "default"
2022-08-28 15:07:37.912  INFO 9408 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-08-28 15:07:37.932  INFO 9408 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-28 15:07:37.932  INFO 9408 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
2022-08-28 15:07:38.084  INFO 9408 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-28 15:07:38.084  INFO 9408 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1071 ms
2022-08-28 15:07:38.382  INFO 9408 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-28 15:07:38.382  INFO 9408 --- [           main] com.learn.spring.StarterClass            : Started StarterClass in 1.739 seconds (JVM running for 2.04)
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-08-28 15:08:01.308  INFO 9408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

但是当我打开浏览器并点击localhost:8080/hello

我收到错误消息网页。 在此处输入图像描述

请指导我哪里出错了。

Your Starter and Hello classes are in different packages ( com.learn.spring vs com.learning.spring.hello ), for Spring to automatically pick up your Hello class it must be in the same package as your Starter .

如果不同的包是这样设计的,你需要告诉 Spring 使用ComponentScan注释来扫描它们:

package com.learn.spring;

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

@SpringBootApplication
@ComponentScan("com.learning.spring")
public class StarterClass {

    public static void main(String[] args) {
        SpringApplication.run(StarterClass.class,args);
    }
}

暂无
暂无

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

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