以下是一个简单的Spring Boot例子:
- 创建一个Maven项目:
mvn archetype:generate -DgroupId=com.example -DartifactId=springboot-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 在pom.xml中添加Spring Boot依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 创建一个Spring Boot应用程序:
@SpringBootApplication
public class SpringBootExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootExampleApplication.class, args);
}
}
- 编写一个RestController:
@RestController
public class HelloWorldController {
@GetMapping("/")
public String helloWorld() {
return "Hello, World!";
}
}
- 运行应用程序:
mvn spring-boot:run
- 在浏览器中打开
http://localhost:8080
,应该看到Hello, World!
的输出。