新建maven工程并添加依赖
新建maven项目,并下载依赖,pom.xml如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建DemoApplication
创建DemoApplication类,并新建main方法
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
运行DemoApplication.main()
控制台打印日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.3.RELEASE)
2021-10-28 15:55:16.084 INFO 22244 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-72NINQO with PID 22244 (D:\learning\SpringBoot\demo\target\classes started by felix in D:\learning\SpringBoot\demo)
2021-10-28 15:55:16.092 INFO 22244 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-10-28 15:55:17.244 INFO 22244 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-28 15:55:17.261 INFO 22244 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-28 15:55:17.261 INFO 22244 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2021-10-28 15:55:17.346 INFO 22244 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-28 15:55:17.347 INFO 22244 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1185 ms
2021-10-28 15:55:17.529 INFO 22244 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-10-28 15:55:17.741 INFO 22244 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-28 15:55:17.745 INFO 22244 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.171 seconds (JVM running for 3.406)
2021-10-28 15:55:18.380 INFO 22244 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-28 15:55:18.380 INFO 22244 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-28 15:55:18.388 INFO 22244 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
一个SpringBoot web项目就运行起来了,此时访问http://localhost:8080,会得到一个404响应
新建org.example.demo.controller.HelloController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class HelloController {
@GetMapping("/hello")
public Object hello() {
return "hello springboot!";
}
}
此时工程结构如下:
重新运行DemoApplication.main,项目启动完成后,访问http://localhost:8080/hello
一个基于SpringBoot的Web工程就搭建完成了