环境约束
–jdk1.8:Spring Boot 推荐jdk1.7及以上;
java version "1.8.0_112"
maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
IntelliJIDEA2018:IntelliJ IDEA 2018.1.4 x64 (IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64 )
SpringBoot 2.1.6RELEASE:(原教材是1.5.9,因为导包存在问题,换成了2.1.3RELEASE版本)
1、新建项目
(1)新建项目
下一步:填入GroupId和ArtifactId,这里groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.snowin,cn表示域为中国,snowin是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.snowin.testProj打头的,如果有个StudentDao,它的全路径就是cn.snowin.testProj.dao.StudentDao。(这段引用https://blog.csdn.net/snowin1994/article/details/53024871)
(3)设置项目名和路径等,不想更改默认即可。
2、导包,在POM文件导入Springboot-starter-parent和spring-boot-starter-web,第一个是所有的springboot项目必须要导入的父项目依赖,第二个是web依赖,我这里因为Springboot1.5.9版本导入有问题,就换成了2.1.6的最新版本,我也没搞明白我这里为啥不行?是跟IDEA2018版本不兼容吗,懂的欢迎留言。
POM代码如下,修改后,reimport 即可。
<?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>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
</dependencies>
</project>
好了,环境搞好了,下来写helloworld.
2、(1)先写主程序:主程序必须标注注解@SpringBootApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 提供
*
* SpringBootApplication 标注主程序类,说明这是一个springboot应用,程序入口
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
(2)写helloworld程序,java下新建Controller.HelloController,新建hello方法,并用@RequestMaping("/hello")映射路径,访问路径为ip+端口号+/hello,
package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(String[] args) {
return "Hello World";
}
}
启动就可运行,这里有一个需要注意的地方,你如果把启动类直接放在的src目录下面,会遇到以下错误,最好放在Controller文件夹下,如上图。
1. 警告解读
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
1
如果你的springboot项目报了这个警告,那么你的项目会无法正常运行。
// 该警告解释为 : ApplicationContext 不能从一个组件的默认包启动
1
2. 如何解决
既然他说该类不能从组件的默认包启动,那就给他建立一个包好了。
// 一般发出这个警告的原因是你把启动类直接放在的src目录下面。
// 你需要在src目录下面再建一个包,比如controlcenter,然后把启动类放到controlcenter下面。
(这里参考https://blog.csdn.net/qq_15071263/article/details/78459087)
3、进行测试,OK,搞定。