经过如下范例,能够快速上手使用SpringBoot框架。先来一个HelloWorld小工程。web
本系列教程均采用 IDEA 做为开发工具,JDK 为 1.8spring
本例可以使用 PostMan工具来进行测试。PostMan 官网地址 可进行下载。浏览器
<!-- 添加SpringBoot 父工程依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent>
<dependencies> <!-- springboot web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
建立包结构信息 com.test.helloworld.controller(存放控制器代码) 、 com.test.helloworld.app(存放启动类代码)springboot
建立控制器类 HelloWorldController 类,代码以下:app
@RestController public class HelloWorldController { @RequestMapping("/") public String sayHello() { return "hello world!"; } }
HelloWorldController类经过注解 @ResController 可将此类进行转变,转变成Rest服务。类中还有一个sayHello函数,此函数经过注解 @RequestMapping 是一个用来处理请求地址映射的注解。框架
@SpringBootApplication public class Application { public static void main(String[] args) { /** * 启动服务 */ SpringApplication.run(Application.class, args); } }
启动 Application 类,成功后 经过浏览器访问 http://localhost:8080/函数
界面会呈现出 hello world ! (表示程序完成)spring-boot