准备工作:
1、Intellij IDEA (ULTIMATE版):官网下载地址 https://www.jetbrains.com/idea/download/#section=windows
2、JDK
一、创建新项目
二、左侧面板选择Spring Initializr
输入项目名称,项目组名称和项目ID,点击进入下一步
下面的页面是用于添加依赖的,可以根据需求,添加依赖。或者在pom.xml文件进行添加也可以。主要包括:Core(核心依赖)、SQL、NOSQL
当前测试只需勾选 Web。
点击Next,项目创建结束。项目架构如下所示:(注:Example.java是我添加的)
二、在相应目录下创建 Example.java。代码如下:
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello "+myName+"!!!";
}
}
三、运行项目,选中 SpringbootTestApplication.java,右击--Run 'SpringbootTestApplication' ,或者点击如图所示按钮:
四、程序成功启动,控制台如下所示(部分):
五、最后我们来测试一下:输入 http://localhost:8080/ 和 http://localhost:8080/hello/王大陆
测试成功!!!
最后,另外附上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.example</groupId>
<artifactId>springboot_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.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>
<!--这个就是我们刚刚勾选依赖时选择的 Web-->
<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>
这是第一次接触 SpringBoot架构,恩,先记下来,免得以后忘记了。
参考博文1:http://blog.youkuaiyun.com/yxl8359026/article/details/51464041
参考博文2:http://blog.youkuaiyun.com/lxhjh/article/details/51711148
注意:参考第二篇博文的时候,因为 pom.xml中没有指定编码方式,然后测试时输入中文的时候出错了。