目錄
一、準備環境
1、Java 7 及以上版本;
2、springboot 1.3.7版本
3、maven 3,2及以上版本;
4、spring Framework 4.2.7及以上版本。
二、構建maven項目過程
本次通過https://start.spring.io/頁面來構建工程,也可以使用idea構建工程(這裏就不演示了)。
將構建好的項目下載之後,通過以上操作導出到idea中。
三、項目目錄解析
單元測試目錄,用不到的話可以刪除。通過上圖可以看出我們工程的結構,src/main/resources:配置目錄,存放配置信息、靜態資源、模板等。
src/main/java:主程序入口SpringBootDemoApplication,可以通過直接運行該類啓動項目。
四、配置解析
<?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 https://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.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.didispace</groupId>
<artifactId>SpringBootDemo</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
五、啓動測試
package com.didispace.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: yd
* @Date: 2019/10/6 12:38
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String index(){
return "Hello World";
}
}
啓動之後,我們通過訪問http://localhost:8080/hello就可以看到helloworld。
六、遇到的坑
1、啓動時IEDA中找不到或無法加載主類 com.example.springbootdome.SpringbootdomeApplication 報這個錯誤;
2、啓動成功後,訪問http://localhost:8080/hello時,This application has no explicit mapping for /error, so you are seeing this as a fallback .報這個錯誤。
解決方案:
1、刪除resources下面的 *.iml文件和根目錄(和src平齊)下的main包。
2、啓動類SpringBootDemoApplication一定要在你邏輯類的上一包節點中。例如:
com.didispace.SpringBootDemoApplication 啓動類
com.didispace.web.HelloController 邏輯類