maven構建項目java
一、訪問http://start.spring.io/web
二、選擇構建工具Maven Project、Spring Boot版本1.3.6以及一些工程基本信息,點擊「Switch to the full version.」 spring
三、點擊Generate Project下載項目壓縮包apache
四、解壓後,使用eclipse,Import -> Existing Maven Projects -> Next ->選擇解壓後的文件夾-> Finsh,OK done!json
項目結構介紹瀏覽器
引入web模塊app
一、pom.xml中添加支持web的模塊:eclipse
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
pom.xml文件中默認有兩個模塊:maven
spring-boot-starter:核心模塊,包括自動配置支持、日誌和YAML;spring-boot
spring-boot-starter-test:測試模塊,包括JUnit、Hamcrest、Mockito。
二、編寫controller內容
@RestController public class HelloWorldController { @RequestMapping("/toIndex") public String index() { return "Hello World"; } }
@RestController的意思就是controller裏面的方法都以json格式輸出,不用再寫什麼jackjson配置的了!
三、啓動主程序,打開瀏覽器訪問http://localhost:8080/hello,就能夠看到效果了,有木有很簡單!
如何作單元測試
結束!
第一天配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>demo</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>demo</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.1.0.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <!--核心配置 --> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter</artifactId> 32 </dependency> 33 <!--測試配置 --> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 <!-- 增長web支持 --> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-web</artifactId> 43 </dependency> 44 <!-- 熱啓動 --> 45 <dependency> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-devtools</artifactId> 48 <optional>true</optional> 49 50 </dependency> 51 </dependencies> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 <configuration> 59 <fork>true</fork> 60 </configuration> 61 </plugin> 62 </plugins> 63 </build> 64 65 66 </project>