1.在Idea上创建基于Spring Boot的web 项目
点击Create New Project选择Spring Initializr并Next
將jar包換成你自己的版本,我的是1.8版本,点击next
我们需要创建web项目,选择web,然后点next直到finish
2.编写controller
在主程序包下新建一个controller包
package com.sample.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
创建成功界面,注意要将Application类放在最外侧,即包含所有子包
然后点击运行。注意:我们之所以在上面的项目中没有手动的去配置 Tomcat 服务器,是因为 Spring Boot 内置了 Tomcat,这时在Console窗口中,我们可以看到如下信息:
我们在浏览器中输入http://localhost:8080/hello