1. 继承 Spring Boot 我的项目
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> <relativePath/></parent>
2. 引入 Spring MVC 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
3. 编写 Controller 代码
@RestControllerpublic class HelloWorldController { @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); }}
4. 编写启动类
@SpringBootApplicationpublic class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); }}
5. 运行启动类
运行启动类,在浏览器中输出:http://localhost:8080/hello?name=小穆
,即可看到如下成果: