SpringBoot入门web项目-helloWorld
文章目录
项目构建
1. 用idea构建Maven项目
file -> new -> maven -> next -> 输入groupId与ArtifactId(可输入springboot) -> next -> finish。
2. 添加maven依赖
在pom.xml
中添加如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 构建SpringBoot启动程序
在src/main/java
新建包com.au
,然后构建MySpringBootApplication
类作为启动类。
package com.au;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName MySpringBootApplication
* @Author Au
* @Date 2019/11/5 14:41
* @Description
**/
// @SpringBootApplication标注该类为启动类
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
此时一个SpringBoot
项目已经构建好了。
4. 创建一个Controller类用于测试
在com.au
下创建controller
包,再创建Controller_test1
类。
package com.au.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName Controller_test1
* @Author Au
* @Date 2019/11/5 14:52
* @Description
**/
@Controller
public class Controller_test1 {
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "hello springboot";
}
}
5. 浏览器测试
因为SpringBoot
已经内嵌了tomcat
,所以可以从浏览器访问,运行MySpringBootApplication
中的main
方法,然后浏览器输入localhost:8080/quick
即可看到hello springboot
字样。
idea快速构建SpringBoot项目
1.
file -> new project -> Spring Initializr -> next -> 输入group等信息 -> next -> Web -> 勾选Spring Web -> 输入项目名 -> finish。
idea设置项目热部署
共三步:
1.
添加pom依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2.
file -> settings -> Build,Execution,Deployment -> Compiler -> 勾选Build project automatically。
3.
ctrl+shift+alt+/ -> Registry -> 勾选compiler.automake.allow.wher.app.running。
然后重启项目即可达到热部署的效果。