在开始之前,我们需要去创建一个SpringBoot项目,大家可以去 http://start.spring.io/ 这个网站生成一个项目。

如图,这边可以对SpringBoot项目进行详细设置:

下面这个web一定要勾选:


SpringBoot版本号选择1.5.10
全部设置好了以后,就点击这个按钮:

这边我就直接下载到桌面了。


解压这个压缩包,我们看下目录结构:

可以看到,这是一个maven项目结构,然后,我们要打开eclipse导入这个项目,建议使用eclipse的mars版本,自带maven插件。
打开eclipse后,导入:



导入桌面上解压后的文件夹:


第一次导入SpringBoot项目,会自动下载很多jar包,建议替换成阿里云的maven仓库,不然会很慢。
wait... ... ...
建议这个时候你去喝杯咖啡~~ 来一局游戏啥的~~
经过漫长的等待,终于好了!!

找到这个类:

这个是SpringBoot项目的启动类,运行:

打开浏览器,输入http://localhost:8080/
发现:

项目给我们抛了一个404,这个是当然的了,因为我们确实什么都没有写啊。
现在,建一个controller包:

建一个HelloController类:

代码:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "<font style='font-size:28px;'>Hello Spring Boot</font>";
}
}
其中,RestController是一个复合注解,相当于 @Controller和@ResponseBody的组合,相信有SpringMVC基础的朋友都能够明白是什么意思。
这里就是返回一个字符串 "Hello Spring Boot" 。
重新启动 DemoApplication 类,在浏览器中输入:
可以看到效果:

您的支持是我写作的最大动力:

个人网站:http://java520.top/
网友评论