docs/Spring全家桶/SpringBoot/SpringBoot的基本使用.md
̳springboot㹻ȨҲ㹻
һġHello WorldκӵĶ˵㡣Ը֣ԸѺõķʽӦ
1һֵIDE,ѡ IntelliJ IDEASpring ToolsVisual Studio Code Eclipse ȵȡ
2JDKڰ汾Ļ8-17Dzѡ
3ȻﻹҪmavenpomҲҪmavenmavenideaԴˡǻڽIJֽнܣҪЩ
ʹstart.spring.ioһwebĿڡdependenciesԻӡwebĻͼʾ
ɡť zip ļѹϵһļС
start.spring.ioĿSpring BootһSpringӦóڲҪ̫á Spring Boot Spring Ŀеķʽ
ѡʹmavenΪߣĿpomļpomļӵ
<?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>3.0.5</version> <relativePath/> <!-- lookup parent from repository --> </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>17</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>
IDE дĿ src/main/java/com/example/demo ļҵ DemoApplication.java ļ
ͨʾĶⷽעļݡԸƲճֱӼ롣
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); }}
Spring Boot дһġHello World Web д롣
ӵhello()ּڻȡһΪnameStringȻ˲еĵ"Hello"
ζнΪAmyӦǡHello Amy
@RestController ע Spring˴һ˵㣬ö˵Ӧ Web Ͽá
@GetMapping(/hello) Spring ʹǵ hello() Ӧ͵ http://localhost:8080/hello ַ
@RequestParam Spring һֵڣĬʹõʡWorld
ǹгУնˣӵĿļļС
ǿͨӦó
MacOS/Linux:
COPY./gradlew bootRun
Windows:
COPY.\gradlew.bat bootRun
ӦûῴһЩ˷dzƵ
иǣSpringӦѾʼˡ Spring Boot Ƕʽ Apache Tomcat 䵱ڼlocalhost˿ڡ8080ϵ
ڶĵַhttp://localhost:8080/hello
ӦõõһѺõĻӦ
˼һSpringBootӦþôˣ㲻ҪļǶķ
ֻҪһ࣬ͿʵһSpringBootӦá
ҲΪʲôspringbootٹһΪʵ̫ˡ
ȻʵʿҪõspringbootĹܺԣǽڽ½չܡ