Spring Boot入门
一、需求
使用IDEA快速构建SpringBoot工程,构建HelloController.hello()方法,返回“Hello Spring Boot”
二、步骤
1. 创建SpringBoot项目(需要网络)
File – New – Module – Spring Initializr
Module SDK选择默认
Group Id:包名(我写的是com.demo)
Artifact Id:项目名(我写的是springboot_init)
Java Version 选择 8
模块选择Spring Web即可
Finish之后,等待IDEA下载好相应文件,即可打开工程。
pom.xml文件已经自动生成,包含<parent>,<dependencies>等配置
除此之外,IDEA还自动写好了启动类SpringbootinitApplication.java
2.编写程序
接下来,只需在com.demo.springboot_init包下写一个HelloController.java即可
HelloController.java
package com.demo.springboot_init;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello Spring Boot";
}
}
3.启动项目
打开SpringbootInitApplication.java,单机main函数左侧绿色三角,运行
浏览器输入:http://localhost:8080/hello
小结
1.创建项目
2.编写HelloController
3.运行项目,浏览器查看效果