前言:
上一篇已经介绍了为什么要使用Spring Boot,这里就不再啰嗦了,如果还有不知道的朋友可以看:
https://blog.csdn.net/hushanfeng110/article/details/79813509
来这家公司快一个月了,真的很看得起我,让我看代码看了一个月。一会看EXTJS,一会又要看Spring Boot,就是不安排事情,我表示有点不爽,三年全栈,一年项目管理,咱要经验有经验,要技术有技术,不管是啥,上来就是干。
准备工作:
- jdk 1.8(建议)
- maven 3.0+(建议)
- Idea(还是建议)
操作步骤:
打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。
目录结构:
- src
-main
-java
-package
-SpringbootApplication
-resouces
- statics
- templates
- application.yml
-test
- pom
- pom文件为基本的依赖管理文件
- resouces 资源文件
- statics 静态资源
- templates 模板资源
- application.yml 配置文件
- SpringbootApplication程序的入口。
pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.forezp</groupId>
<artifactId>springboot-first-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-first-application</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关键代码:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Spring Boot,helloworld!";
}
}
结果:
运行SpringbootFirstApplication的main方法,打开浏览器localhost:8080,显示
Spring Boot,helloworld!