为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Boot URL 请求映射返回 NullPointerException

Spring Boot URL 请求映射返回 NullPointerException

动漫人物 2023-03-02 14:58:52
我正在尝试部署一个非常简单的 Spring Boot 应用程序,其中导航到 localhost:8080/greetings 会显示“来自 Spring Boot 的问候语!”。但是,该 URL 似乎不起作用。导航到链接提示我出现空指针异常。我对这个问题做了一些初步研究,但以下修复没有奏效:将类从默认包中移出并移入定义的包中使用@ComponentScan将我的控制器类移动到我的应用程序类的子包控制器类:package controllers;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @RequestMapping("/greetings")    public String index() {        return "Greetings from Spring Boot!";    }}应用类:package controllers;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-spring-boot</artifactId><version>0.1.0</version><parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.1.4.RELEASE</version></parent>   <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <java.version>1.8</java.version></properties><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency></dependencies></project>
查看完整描述

2 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

我找到了答案。原来我需要<groupId>在我的 pom.xml 中制作唯一的



查看完整回答
反对 回复 2023-03-02
?
慕森王

TA贡献1777条经验 获得超3个赞

无论如何,在 servlet 容器或应用程序服务器中运行 spring-boot 应用程序有什么意义?

您最初发布的应用程序在mvn spring-boot:run使用java -jar ./target/gs-spring-boot-0.1.0.jar.

使用 spring-boot 的全部意义在于拥有一个嵌入式容器以便独立运行应用程序。如果不是这样,我建议减少依赖性,因为当部署到 servlet 容器(例如 Tomcat)或应用程序服务器(例如 JBoss)时,您的应用程序可以变得更小;即在 KB 范围内而不是 16MB 范围内。


查看完整回答
反对 回复 2023-03-02
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信