使用maven创建第一个SpringBoot项目
第一步:
第二步:
第三步:
第四步:
在pom文件中添加父依赖,控制SpringBoot的版本,和web依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cc</groupId>
<artifactId>firstSpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
第五步:
在src下创建项目项目工程相关包,并写启动类
-
目录结构:
-
启动类:
package com.cc.firstSpringBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class mainApplication {
public static void main(String[] args) {
SpringApplication.run(mainApplication.class,args);
}
}
第六步:
在controller包中创建一个访问web应用的类
package com.cc.firstSpringBoot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("hi")
public @ResponseBody String say(){
return "Hello Spring Boot!";
}
}
第七步:
启动项目
第八步:
访问http://localhost:8080/hi
热部署:
第一步:打开顶部工具栏 File -> Settings -> Default Settings -> Build -> Compiler 然后勾选 Build project automatically
第二步:
同时按住 Ctrl + Shift + Alt + / 然后进入Registry ,勾选自动编译
第三步:在pom中热部署依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
第四步:打开谷歌浏览器,打开F12的Network选项栏,然后勾选【✅】Disable cache 。
最后重启项目:修改代码,保存代码,自动重新加载项目。
拓展:
简单的RESTFul架构
在HelloController类中添加下代码
@RequestMapping(value = "user/{id}")
public @ResponseBody String userId(@PathVariable String id){
return "userId:"+id;
}
/{}
就是restful的简单方式
访问http://localhost:8080/user/123,123是任意的,对应参数id