Java Spring启动应用程序错误页

Java Spring启动应用程序错误页,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我开始学习SpringBoot,并在youtube上学习教程。然而,在我的项目中发生了一件奇怪的事情。我刚刚创建了一个名为GreetingController的控制器。下面是该类的完整代码 @RestController @EnableAutoConfiguration public class GreetingController { private static BigInteger nextId; private static Map<BigInteger, Gr

我开始学习SpringBoot,并在youtube上学习教程。然而,在我的项目中发生了一件奇怪的事情。我刚刚创建了一个名为GreetingController的控制器。下面是该类的完整代码

@RestController
@EnableAutoConfiguration
public class GreetingController {

    private static BigInteger nextId;

    private static Map<BigInteger, Greeting> greetingMap;

    private static Greeting save(Greeting greeting) {
        if (greetingMap == null) {
            greetingMap = new HashMap<BigInteger, Greeting>();
            nextId = BigInteger.ONE;
        }
        greeting.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        greetingMap.put(greeting.getId(), greeting);
        return greeting;
    }

    static {
        Greeting g1 = new Greeting();
        g1.setText("Hello World");
        save(g1);

        Greeting g2 = new Greeting();
        g2.setText("Hola Mundo");
        save(g2);
    }

    @RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Greeting>> getGreetings() {

        Collection<Greeting> greetings = greetingMap.values();

        return new ResponseEntity<Collection<Greeting>>(greetings,
                HttpStatus.OK);

    }

}
@RestController
@启用自动配置
公共类迎宾控制器{
私有静态大整数nextId;
私有静态映射迎宾映射;
专用静态问候语保存(问候语){
if(greetingMap==null){
greetingMap=新HashMap();
nextId=biginger.ONE;
}
问候语.setId(nextId);
nextId=nextId.add(biginger.ONE);
greetingMap.put(greeting.getId(),greeting);
回敬问候;
}
静止的{
问候语g1=新问候语();
g1.setText(“你好世界”);
保存(g1);
问候语g2=新问候语();
g2.setText(“Hola Mundo”);
保存(g2);
}
@RequestMapping(value=“/api/greetings”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
公众反应{
集合问候语=greetingMap.values();
返回新的响应(问候,
HttpStatus.OK);
}
}
控制器位于以下程序包下:

但是,当我使用URL
http://localhost:8080/api/greetings
我的页面上出现以下错误:

但是,当我将GreetingController放在应用程序类的同一个包中时,如下图所示:

然后得到相同的URL
http://localhost:8080/api/greetings
,我得到了正确的回答:


有人能解释一下原因吗?

将您的
com.example
包重命名为
org.example
。当您放置
@SpringBootApplication
注释时,springboot会扫描类包的所有子包


或者将
@ComponentScan(“org.example”)
放在同一个类上。通过这种方式,您可以告诉spring boot在哪里搜索控制器(和其他bean)。

com.example
包重命名为
org.example
。当您放置
@SpringBootApplication
注释时,springboot会扫描类包的所有子包


或者将
@ComponentScan(“org.example”)
放在同一个类上。通过这种方式,您可以告诉spring boot在何处搜索控制器(和其他bean)。

如果您希望支持在另一个包中包含控制器,则需要将其包含在应用程序的组件扫描中

Application.java
中,可以添加以下内容:


@组件扫描({com.example,org.example})

默认情况下,应用程序所在的包将包含在ComponentScan中,这就是为什么当控制器与应用程序位于同一个包中时,它可以为您工作的原因


此外,您不需要控制器上的
@EnableAutoConfiguration
注释,仅供参考。

如果您希望支持在另一个包中安装控制器,则需要将其包含在应用程序的组件扫描中

Application.java
中,可以添加以下内容:


@组件扫描({com.example,org.example})

默认情况下,应用程序所在的包将包含在ComponentScan中,这就是为什么当控制器与应用程序位于同一个包中时,它可以为您工作的原因


此外,您不需要控制器上的
@EnableAutoConfiguration
注释,仅供参考。

您是否设置了视图解析程序以找到正确的路径?是否设置了视图解析程序以找到正确的路径?