环境: win 10 、IDEA 2018.2.8、jdk 1.8 。
准备工作: 把Intelij IDEA 内置 maven 修改为国内镜像(阿里),这一步看个人情况,我是用 IDEA 自带的 maven,所以改了一下,觉得自己的没问题,速度够快就不用改。想改的参考着篇博客:在Intelij IDEA中修改maven为国内镜像(阿里)
1 使⽤用 IDEA 新建项目:
(1)选择 File | New | Project… 命令,选择新建项目对话框。
(2)看图操作:
(3)项目结构:
(4)手动在 pom.xml 中添加:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(5)在com.example.hello.hello 下新建文件夹 web,并在改文件夹下创建 HelloController。
@RestController //@RestController 的意思是 Controller ⾥里里⾯面的⽅方法都以 JSON 格式输出,不不需要有其他额外的配置;如
果配置为 @Controller,代表输出内容到⻚页⾯面。
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
小提示: 如果出现 Cannot resolve symbol ‘RestController’ ,(把鼠标光标定位到错误处)在错误处按alt+enter 选择add spring-boot-start-web to classpath
(6)启动主程序:右键单击项⽬目中的 HelloApplication | run 命令,就可以启动项⽬目了了,若出现以下内容表示启动成功:
main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
main] c.example.hello.hello.HelloApplication : Started HelloApplication in 3.482 seconds (JVM running for 5.625)
(7)启动成功后,打开浏览器输入:http://localhost:8080/hello 就可以看到:hello world,
到这⾥,我们的第一个 Spring Boot 项目就开发完成了了,有没有感觉很简单?经过测试发现,修改 Controller内相关的代码,需要重新启动项目才能生效,这样做很麻烦是不不是?别着急,Spring Boot ⼜又给我们提供了了另外一个组件来解决。
热部署:
热启动就需要⽤用到我们在一开始就引⼊入的另外一个组件:spring-boot-devtools。它是 Spring Boot 提供的⼀组开发⼯具包,其中就包含我们需要的热部署功能,在使⽤用这个功能之前还需要再做一些配置。
1 添加依赖
在 pom.xml 文件中添加 spring-boot-Devtools 组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>