参考文档:
spring-boot官方文档,基于1.5.6.release版本。原版文档链接:
`https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/`;
1.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Additional lines to be added here... -->
</project>
2.启动类
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
代码解释:
@RestController:标识该类为rest风格的controller;
@RequestMapping:接口的url映射;
以上两个都属于spring框架中的配置;
@EnableAutoConfiguration:让spring-boot自动配置项目,由于pom.xml文件中添加了spring-boot-starter-web
,所以将会自动配置为一个web项目;
3.启动项目
运行main方法,项目启动后,在浏览器中输入`http://localhost:8080`,可返回`hello world`;