使用Spring Boot的Intelij创建一个HelloWorld应用程序
步驟
-
- 在InteliJ中,转到「文件」→「新建」→「项目」。
-
- 在打开的新项目中,选择「Spring Initializer」,然后点击「下一步」。
-
- 随意输入「组」和「构件」。由于想要部署到服务器上,选择「打包方式」为「War」,然后点击「下一步」。
-
- 由于想要创建一个Web应用程序,勾选「Web」→「Spring Web」,然后点击「下一步」。
-
- 随意输入「项目名称」和「项目位置」,然后点击「完成」。
- 在自动生成的DemoApplication中,按照以下方式添加内容,并运行。
我没有@RestController,遇到了困难…
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+ import org.springframework.web.bind.annotation.GetMapping;
+ import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
+ @RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
+ @GetMapping("/hello")
+ public String sayHello() {
+ return "Hello World";
+ }
}
- 如果访问以下URL,应该会显示“Hello World”。
http://localhost:8080/hello 的意思是本地主机的8080端口上的hello。
