目录
一、准备环境
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 逻辑类