目录
一、准备环境
二、构建maven项目过程
三、项目目录解析
四、配置解析
五、启动测试
六、遇到的坑
1、Java 7 及以上版本;
2、springboot 1.3.7版本
3、maven 3,2及以上版本;
4、spring Framework 4.2.7及以上版本。
本次通过https://start.spring.io/页面来构建工程,也可以使用idea构建工程(这里就不演示了)。
将构建好的项目下载之后,通过以上操作导出到idea中。
单元测试目录,用不到的话可以删除。通过上图可以看出我们工程的结构,src/main/resources:配置目录,存放配置信息、静态资源、模板等。
src/main/java:主程序入口SpringBootDemoApplication,可以通过直接运行该类启动项目。
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.3.7.RELEASE
com.didispace
SpringBootDemo
jar
0.0.1-SNAPSHOT
SpringBootDemo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
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 逻辑类