给我一个springboot的简单示例
时间: 2023-07-20 11:41:44 浏览: 52
好的,以下是一个简单的Spring Boot示例,它创建了一个名为“Hello World”的RESTful API:
首先,创建一个名为“HelloWorldApplication”的Spring Boot应用程序类,并添加@SpringBootApplication注释:
```java
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
```
接下来,创建一个名为“HelloController”的REST控制器类,并添加@RestController和@RequestMapping注释:
```java
@RestController
public class HelloController {
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
}
```
最后,在Maven或Gradle构建文件中添加Spring Boot依赖项:
Maven:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
Gradle:
```groovy
implementation 'org.springframework.boot:spring-boot-starter-web'
```
现在,运行应用程序并访问http://localhost:8080,您应该能够看到“Hello World!”的响应。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)