spring-boot版helloworld
时间: 2023-11-20 07:05:50 浏览: 38
以下是使用Spring Boot实现的helloworld示例代码:
```java
import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class HelloWorldController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(HelloWorldController.class, args);
}
}
```
这段代码使用了Spring Boot的注解来实现一个简单的helloworld程序。其中,`@Controller`注解表示这是一个控制器类,`@EnableAutoConfiguration`注解表示自动配置Spring Boot应用程序,`@RequestMapping("/")`注解表示处理根路径的请求,`@ResponseBody`注解表示返回的是响应体内容。
你可以将这段代码保存为`HelloWorldController.java`文件,然后使用Maven或Gradle等构建工具进行构建和运行。