新安装了IntellJ IDEA,第一次创建Spring boot项目时,发生无法请求https://start.spring.io的问题,
解决办法:
File->Settings 搜索HTTP
然后输入http://start.spring.io
提示successful 即表示成功,这丝重新创建项目均可!
环境:java1.8 ,Maven-3.6.1,SpringBoot 2.x
工具:IDEA
当然也可以创一个Maven空项目,导入SpringBoot的依赖,创建启动类,但是嘛,新手就是用一体化创建的不香吗?
创建新项目,选择spring initalizr,然后点击Next。
填写项目的信息,继续Next
选择初始化的组件(初学勾选 Web 即可),继续Next
最后Finish创建项目
项目结构分析
1.BoottestApplication.java:boot项目的启动类
2.static:存放静态资源的地方,例如css,js,image…
3.templates:存放前端模板的地方,就是前端的html页面
4.application.properties:项目的配置文件,也可用application.yml的格式来写。
5.pom.xml:项目的组件依赖,利用maven从远程仓库下载到本地项目。
pom.xml分析
<!-- 项目的父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- web项目的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- 剔除依赖 -->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我们来个helloworld,感受一下Springboot。
到在主程序的同级目录下,新建一个controller包,一定要在同级目录下,否则识别不到.
在controller创建一个HelloController类
注解应该看得懂吧?springMVC的知识哦,@RestController表示这是一个返回字符串或者JSON数据的控制类,@RequestMapping("/hello")是页面请求路径 http://localhost:8080/hello; springboot的端口号默认为8080,当然可用取配置文件里面改哦.
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello World!!!";
}
}
编写完毕,运行启动类,打开浏览器,输入网址进行访问。
启动成功如下,然后到浏览器访问地址http://localhost:8080/hello ,图中所指就是当前项目启动所用的端口号,没配置默认为8080