SpringBoot
为微服务而生,大大简化搭建web
工程的时间;我的理解,springboot
是一些框架的集合, 整合各个框架,下降使用门槛。java
介绍项目的同时,推荐相关IntelliJ IDEA
快捷键,熟能生巧,无需死记硬背。git
web
服务点击File -> new -> project,或者快捷键
ALT+F
github
选择Maven -> nextweb
填写项目信息 -> nextspring
- src -main -java -com.mkeeper #web入口目录 -controller #/hello web服务 -HelloController #主函数,启动类,运行它若是运行了 Tomcat、Jetty、Undertow 等容器 -Chapter111Application #资源文件存放目录 -resouces #主要的配置文件,SpringBoot启动时候会自动加载application.yml/application.properties -application.yml #测试文件存放目录 -test #pom.xml 文件是Maven构建的基础,里面包含了咱们所依赖JAR和Plugin的信息 -pom #编译后产生的文件 -target #idea工程文件,忽略,不要修改 -Chapter1-1-1.iml
添加pom.xml 依赖apache
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkeeper</groupId> <artifactId>Chapter1-1-1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Chapter1-1-1</name> <description>Hello Spring Boot</description> <!--版本统一采用 2.0.3.RELEASE --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- 惟一须要依赖,默认就内嵌了Tomcat容器,如须要更换容器Jetty、Undertow也极其简单--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
新建主函数:java -> 右键 -> New -> Java Class,或者快捷键
ALT+Insert
windows
package com.mkeeper; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Chapter111Application { public static void main(String[] args) { SpringApplication.run(Chapter111Application.class, args); } }
新建HelloController,快捷键
ALT+Insert
springboot
package com.mkeeper.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; //@RestController 等同于 (@Controller 与 @ResponseBody) @RestController public class HelloController { //@GetMapping 等同于 (@RequestMapping(method = RequestMethod.GET)) @GetMapping("/hello") public String hello(){ return "Hello SpringBoot"; } }
启动服务,Chapter111Application -> 右键 -> Run,或者快捷键
Shift+F10
app
Ctrl+F2
打开Postman,输入网址localhost:8080/hello框架
开篇文章,多多包涵,有任何建议,欢迎留言探讨,源码。
欢迎关注博主公众号:Java十分钟