1、新建一個Maven工程java
(1)選擇建立簡單MAVNE工程web
(2)輸入你本身的MAVEN工程的Group Id(必填)、Artifact Id(必填)、Version(必填)、Packaging(必填)、Name以及Description。spring
在Parent Project 中輸入SpringBoot的Group Id:org.springframework.boot、Artifact Id: spring-boot-starter-parent 以及 Version:1.5.6.RELEASE(版本能夠本身選擇)。apache
(3)編寫pom.xml瀏覽器
本人在pom.xml中定義了java版本爲1.8的,項目建立時默認爲1.6的,若是想改變java版本,記得 右鍵項目 --> Maven --> Update Project一下。app
Maven中引入 web相關的 spring-boot-starter-web ,版本不須要本身定義,boot 默認給你配置最優的版本。maven
<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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <groupId>org.rcddup</groupId> <artifactId>rcddup-custom</artifactId> <version>1.0.0</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
(4)建立一個啓動的java文件,作爲SpringBoot的主入口spring-boot
SpringApplication.run()方法:該方法作爲SpringBoot程序的主入口,只要運行此方法,整個程序就啓動了,是否是很方便。spa
package org.rcddup.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class App { @RequestMapping("/") String home() { return "<h1>hello rcddup</h1>"; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
(5)啓動 Appcode
啓動 App.java 文件以後,會有一個大的Spring橫幅出來,帶有SpringBoot的版本信息,是否是很酷炫。SpringBoot默認使用Tomcat啓動,默認端口號爲:8080。
(6)打開瀏覽器,輸入:localhost:8080,便可查看到後臺返回的字符串。
下面對上述中使用的註解給出說明:
一、@SpringBootApplication:該註解至關於@Configuration
, @EnableAutoConfiguration
和 @ComponentScan三個註解的效果,該是SpringBoot爲了方便開發者而給出的一個便捷方式。
(1)@Configuration:標記的這個類能夠作爲 Spring 的配置類,添加 @Import 註解可以使其正真成爲 Spring 的配置類。
(2)@EnableAutoConfiguration:啓動自動配置。
(3)@ComponentScan:組件掃描註解。
二、@RestController:該註解是另外一個很是方便你們使用的註解,至關於 @Controller和 @ResponseBody.,爲@RestController類下面的全部方法,添加 @ResponseBody註解。
三、@RequestMapping:定義用戶請求的路由規則,能夠添加到類上,至關於爲該類下的每一個方法添加路由前綴。