IDEA上用Spring Boot构建第一个hello world项目

环境: 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值