关于spring-mvc:从零搭建-Spring-MVC-项目-HelloWorld

0次阅读

共计 721 个字符,预计需要花费 2 分钟才能阅读完成。

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 代码

@RestController
public class HelloWorldController {@GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {return String.format("Hello %s!", name);
    }

}

4. 编写启动类

@SpringBootApplication
public class HelloWorldApplication {public static void main(String[] args) {SpringApplication.run(HelloWorldApplication.class, args);
    }

}

5. 运行启动类

运行启动类,在浏览器中输出:http://localhost:8080/hello?name= 小穆 ,即可看到如下成果:

正文完
 0