快速入门
快速的搭建SpringBoot的入门级工程,使用SpringBoot来构建Spring项目
需求:搭建SpringBoot工程,定义HelloController.hello()方法,返回Hello SpringBoot
传统方式构建SpringBoot工程
实现步骤:
创建
Maven项目打开
IDEA,点击File-->New-->Module-->Maven-->Next在
GroupId中输入:com.jlc;在ArtifactId中输入:springboot-helloworld;再点击Next在
Module name中输入:springboot-helloworld;再点击Finish导入
SpringBoot起步依赖xml<?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>com.jlc</groupId> <artifactId>springboot-helloworld</artifactId> <version>1.0.SNAPSHOT</version> <!--springboot工程需要继承的父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> <dependencies> <!--Web开发的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>定义
Controller在
src/main/java中创建javapackage com.jlc.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "Hello SpringBoot"; } }编写引导类(引导类可以理解为
SpringBoot项目的入口,其后缀一般都是Application结尾的)javapackage com.jlc; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; @SpringBootApplication // 编写引导类需要添加该注解 public class HelloApplication { public static void main(String[] args) { // 后续运行SpringBoot项目直接运行main方法即可 SpringApplication.run(HelloApplication.class, args); } }启动测试和访问
在浏览器中输入:
localhost:8080/hello进行访问,页面显示:Hello SpringBoot
小结:
SpringBoot在创建项目时,使用jar的打包方式SpringBoot的引导类,是项目的入口,运行main方法就可以启动项目- 使用
SpringBoot和Spring构建项目,业务代码编写方式完全一样
使用IDEA快速构建SpringBoot工程
使用IDEA快速构建SpringBoot工程是需要进行网络连接的,在没有网络的情况下是不能构建的
定义HelloController.hello()方法,返回Hello SpringBoot
打开IDEA,点击File-->New-->Module-->Spring Initializr-->选择SDK(JDK)版本-->Next
在Group中输入:com.jlc;在Artifact中输入:springboot-init;在Type中选择Maven Project(表示选择的是Maven的项目);在Language中选择Java;打包方式Packaging中选择:jar;Java Version中选择对应的版本,其他配置可以默认,最后点击Next
如果我们要做Web开发,我们选择Web,勾选相关的依赖:Spring Web-->Next
输入工程的名称,可以使用默认的-->Finish
通过IDEA快速构建SpringBoot工程,会从网络中下载相关的依赖,在pom.xml中会自动的引入相关的坐标
同时在src/main/java/com/jlc/springbootinit文件夹中,会自动创建相应的引导类(不需要我们进行手动的编写):SpringbootInitApplication,其内容为:
package com.jlc.springbootinit;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
@SpringBootApplication
public class SpringbootInitApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootInitApplication.class, args);
}
}我们只需要编写Controller文件即可:
package com.jlc.springbootinit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello SpringBoot";
}
}