Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。
1.首先介紹極簡的helloworld工程
1).打開Eclipse,新建Maven工程,修改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.enn</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-sample-mysql</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)、編制Application.java存於myFirstProject\src\main\java\com\example\myFirstProject下
- package com.example.myFirstProject;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
- package com.example.myFirstProject;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @EnableAutoConfiguration
- public class Example {
- @RequestMapping("/")
- String home() {
- return "Hello World!";
- }
- @RequestMapping("/hello/{myName}")
- String index(@PathVariable String myName) {
- return "Hello "+myName+"!!!";
- }
- }
4).再次點擊“OK”按鈕,在Eclipse的Console中開始打印如圖5
5)、打開瀏覽器,輸入http://localhost:8080,顯示如圖6
6)、在瀏覽器中,輸入http://localhost:8080/hello/SpringBoot
2、使用官網樣例創建工程
一、在瀏覽器中打開http://start.spring.io/,如圖
在Artifact中輸入spring-boot-sample-helloworld,點擊“Switch to the full version.”,勾選"web",然後點擊“ Generate Project alt +”按鈕,把文件保存到本地某個位置
二、下載文件導入eclips
1、解壓下載的文件到某個文件夾;
2、在eclips中導入工程file->import->Import Existing Maven Projects-->Select Maven projects-->finish
3、在eclips中運行工程,正確則入圖
三、添加HelloController
1、在包com.example中點擊右鍵,選擇new->class,再在Name中輸入HelloController
2、在類上面條件聲明@RestController
3、增加方法
- @RequestMapping("/")
- public String helloworld(){
- return "Hello world!";
- }
- @RequestMapping("/hello/{name}")
- public String hellName(@PathVariable String Name){
- return "Hello "+Name;
- }
4、保存Ctrl+S
5、自動添加引用 Ctrl+SHift+O
6、整個HelloController文件如下
- package com.example;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class HelloController {
- @RequestMapping("/")
- public String helloworld(){
- return "Hello world!";
- }
- @RequestMapping("/hello/{name}")
- public String hellName(@PathVariable String name){
- return "Hello "+name;
- }
- }
在瀏覽器中依次輸入
http://localhost:8080/
http://localhost:8080/hello/上帝