1

我正在尝试编写一个示例 Spring Boot 应用程序。

我最初的想法是这是一个易于遵循和编写的示例,但我无法在到达终点时打印出简单的语句。

我现在不想使用任何视图(HTML、JSP 等),因为我想先学习后端。

我已关注:

但是,经过一整天的谷歌搜索后仍然卡住了。

所以让我重申一下这些步骤,我做到了。

  1. 初始化一个 spring boot 项目spring.io并从zip.
  2. 提取 zip 并将其导入 Eclipse。
  3. 配置了一个带有端点的 Rest Controller 以仅打印一个 hello world 语句。
  4. Tomcat 9.0.56存在于我的系统上。
  5. 将 TodoApplication 作为 Java 应用程序或 Spring 启动运行。
  6. 我打的网址是http://localhost:8080/greet
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @GetMapping("/greet")
    public String sayHelloWorld() {
        return "Hello World";
    }
}

春季启动应用程序:

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

@SpringBootApplication
public class TodoApplication {

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

}

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.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sample</groupId>
    <artifactId>todo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>todo</name>
    <description>Back-end for To DO App</description>

    <properties>
        <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>

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

浏览greet网址后:

URL 不存在错误

在控制台出现以下异常:

Spring Boot 圆形视图错误

4

1 回答 1

1

所以xerx593指出的解决方案如下:

  1. 确保在https://start.spring.io/中选择打包为 war/jar 。
  2. 导入压缩文件。
  3. 在 SpringBootApplication 的相同或子包中添加您的控制器类,请参阅此以获取更多信息https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code .
  4. 打开终端并将目录更改为工作文件夹。
  5. 如果使用 maven,则运行 mvn spring-boot:run。
  6. 点击端点,例如 http://localhost:8080/greet 或 curl 请求。

另外我注意到 8080 端口正在侦听但 spring boot 无法在其上注册,在这种情况下设置

server.port = port other than 8080 

在 application.properties 中。

8080收听

应用程序属性文件

于 2022-01-14T15:47:05.727 回答