Spring Boot入门
Spring Boot入门
IDEA创建SpringBoot
创建SpringBoot工程
更新:注意这里不小心错了,要选择Maven Project
创建之后是下面的页面
添加web功能起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这个时候就已经创建了一个SpringBootWeb工程了,启动SpringBoot引导类,src/man/java/com.example.demo中的DemoApplication.
启动之后控制台会显示如下信息,
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
2020-08-15 11:32:49.154 INFO 15816 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on 眸笑 with PID 15816 (D:\文件\JavaStudy\springboot\demo\target\classes started by Administrator in D:\文件\JavaStudy\springboot\demo)
2020-08-15 11:32:49.156 INFO 15816 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-08-15 11:32:49.813 INFO 15816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-15 11:32:49.820 INFO 15816 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-15 11:32:49.820 INFO 15816 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-15 11:32:49.893 INFO 15816 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-15 11:32:49.893 INFO 15816 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 702 ms
2020-08-15 11:32:50.032 INFO 15816 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-15 11:32:50.160 INFO 15816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-15 11:32:50.167 INFO 15816 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.314 seconds (JVM running for 2.636)
在浏览器输入localhots:8080
显示下面界面之后就代表启动成功了。
现在来做一个简单的HelloWorld!
- 创建一个SayHelloController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SayHelloController {
@RequestMapping("/hello")
@ResponseBody
public String sayHello(){
return "Hello World!";
}
}
- 重启SpringBoot引导类,然后再浏览器输入http://localhost:8080/hello就可以访问了
热部署
添加依赖
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
如果使用IDEA热部署失败可以参考这篇文章