SpringBoot简单使用:快速入门
简介
创建一个Spring Boot应用,并且实现一个简单的Http请求处理。
环境
Windows 10 专业版
Intellij IEDA 2018.1.6
apache-maven-3.3.9
jdk1.8.0_45
Postman-win64-7.0.6
使用Intellij IDEA创建
第一步 新建project
第二步 Next,等待片刻后,进入Project Metadata窗口,修改Group和Artifact
第三步 Next, 进入选择Spring Boot版本和依赖管理的窗口
第四步 Next 选择项目的保存路径
第五步 Finsh 项目创建完成
项目结构解析
Spring Boot的基础结构共三个文件
1.SpringbootApplication 程序的启动文件
2.application.properties 配置文件
3.SpringbootApplicationTests 测试文件
项目依赖解析
<?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>
<!--springboot依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--此项目坐标模块-->
<groupId>com.liuym</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<!--依赖版本模块-->
<properties>
<java.version>1.8</java.version>
</properties>
<!--依赖模块-->
<dependencies>
<!--spring-boot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spirng-boot-test-->
<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>
<!--maven打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
http请求接口
第一步 新建controller包
第二步 创建HelloController类
package com.liuym.springboot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author liuym
* @Date 2020/7/10 13:37
*/
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "hello spring boot";
}
}
1.@RestController注解是@Controller和@ResponseBody的组合
2.@GetMapping注解是@RequestMapping并且请求方式是GET
第三步 选择启动程序SpringbootApplication类启动
简单的springboot项目创建完成
打开postman或者浏览器访问 http://localhost:8080/hello
,返回hello spring boot