intellij-idea Sping Boot 基本项目未在Intellij Community Edition中运行相同的代码在STS中运行

3yhwsihp  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(146)

我已经在IntelliJ community Edition中创建了一个基本的Spring项目,其中包含以下三个文件。
入门级:

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

Hello.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
我看到错误消息网页。x1c 0d1x
请告诉我我哪里做错了。

k4ymrczo

k4ymrczo1#

您的StarterHello类位于不同的包中(com.learn.springcom.learning.spring.hello),为了让Spring自动拾取您的Hello类,它必须与您的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);
    }
}

相关问题