020_第一个SpringBoot程序
开发环境
快速搭建的两种方案
Spring官网生成项目,导入IDEA
IDEA快速创建(集成官网生成项目)
Package name可以改为:com.qing
关键文件
程序的主入口Application.java
SpringBoot核心配置文件application.properties
单元测试ApplicationTests.java
编码测试
创建包,程序主入口同级目录Application.java
只有在主入口同级目录下才会自动扫包
创建第一个Controller及接口
package com.qing.helloword.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
// 调用:http://localhost:8080/hello
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
运行Application.java
打包 mvn package
运行jar包 java -jar ./helloword-0.0.1-SNAPSHOT.jar
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
D:\code\springboot\helloword\target>java -jar ./helloword-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.0)
2021-11-28 16:33:03.539 INFO 8048 --- [ main] com.qing.helloword.HellowordApplication : Starting HellowordApplication v0.0.1-SNA
PSHOT using Java 1.8.0_271 on L87Y12K91TH8M2R with PID 8048 (D:\code\springboot\helloword\target\helloword-0.0.1-SNAPSHOT.jar started by Adm
inistrator in D:\code\springboot\helloword\target)
2021-11-28 16:33:03.549 INFO 8048 --- [ main] com.qing.helloword.HellowordApplication : No active profile set, falling back to d
efault profiles: default
2021-11-28 16:33:08.557 INFO 8048 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (h
ttp)
2021-11-28 16:33:08.639 INFO 8048 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-11-28 16:33:08.640 INFO 8048 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/
9.0.55]
2021-11-28 16:33:08.951 INFO 8048 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicat
ionContext
2021-11-28 16:33:08.953 INFO 8048 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initializati
on completed in 5074 ms
2021-11-28 16:33:10.194 INFO 8048 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) w
ith context path ''
2021-11-28 16:33:10.222 INFO 8048 --- [ main] com.qing.helloword.HellowordApplication : Started HellowordApplication in 8.587 se
conds (JVM running for 13.07)
2021-11-28 16:33:10.222 INFO 8048 --- [ main] com.qing.helloword.HellowordApplication : Started HellowordApplication in 8.587 se
conds (JVM running for 13.07)
2021-11-28 16:34:20.227 INFO 8048 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'd
ispatcherServlet'
2021-11-28 16:34:20.228 INFO 8048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-11-28 16:34:20.230 INFO 8048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
更改端口号
server.port=8081
更改启动图标springboot banner
banner在线生成网站:https://www.bootschool.net/ascii
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.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qing</groupId>
<artifactId>helloword</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloword</name>
<description>helloword</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>
分类:
150_SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了