1. 明確開發環境
Spring Tool Suite
Version: 3.9.2.RELEASE
2. 創建Maven項目
File–>New–>Maven Project
3.配置pom.xml文件
在pom.xml文件中進行以下修改:
<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.iamapsycho</groupId>
<artifactId>SpringBootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>billingBootTest-iamapsycho</name>
<description>Demo project for Spring Boot</description>
<!--
spring-boot-starter-parent是Spring Boot的核心啓動器,
包含了自動配置、日誌和YAML等大量默認的配置,大大簡化了我們的開發。
引入之後相關的starter引入就不需要添加version配置,
spring boot會自動選擇最合適的版本進行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!-- spring-boot-starter-web包含了Spring Boot預定義的一些Web開發的常用依賴包
如: spring-webmvc,Tomcat.... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.創建 Application.java 啓動類
在src/main/java下 創建包com.iamapsycho,並在包內創建Application.java
package com.iamapsycho;
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);
}
}
5.創建HelloWorld的Controller類
在src/main/java下 創建包com.iamapsycho.controller,並在包內創建HelloWorldController.java
package com.iamapsycho.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorldController {
@ResponseBody
@RequestMapping("/helloworld")
public String helloWorld(){
return "Hello World! This is Spring Boot !!!";
}
}
6.運行並測試
接下來就運行Application.java,然後訪問地址:
http://localhost:8080/helloworld
7.添加項目SpringBoot yml 配置 - 項目名稱
當需要在spring boot 項目中定義一些變量,設置參數時候,可以在application.yml中進行設置
application.yml 當前活躍的爲dev(開發環境)
現在訪問 http://localhost:8080/helloworld 將會報錯,
應加上項目名稱
http://localhost:8080/SpringBootTest/helloworld