Spring Initializr:一个快速构建springboot项目的网站
进入网站后,选择:
- Project: Maven
- Language: Java
- Spring Boot: 最新稳定版
- Dependencies: Spring Web
生成的文件结构类似于:
my-spring-boot-app
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── MySpringBootApplication.java
│ │ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── MySpringBootApplicationTests.java
├── pom.xml
暂时忽略test文件,修改MySpringBootApplication.java文件
编写主类:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
在同样位置创建控制器HelloController.java
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
编译运行MySpringBootApplication.java
然后打开打开浏览器访问 http://localhost:8080/hello
你应该就能看到下面的界面了:
现在,你已经成功向世界问好!继续前进吧~