SpringBoot入门案例分为以下几个步骤:
1.创建Maven工程,创建simple project,类型为jar。
2.修改JDK的版本, 必须是JDK1.8以上的版本方可用。
3.构建全新的Pom.xml,
//添加spring-boot-starter-parent父类jar,统一管理jar包。
<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>
//定义该项目是web项目
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4.创建HelloController类, 包名:cd.msds.controller
package cd.msds.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//改为restful格式
@RequestMapping("/ps/{name}")
public String hello(@PathVariable String name) {
return name+"我觉得你长得真美";
}
5.创建启动了RunApplication,
package cd.msds.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class, args);
}
}
- 启动RunApplication
注意:访问时, 不需要输入项目名称,输入:http://localhost:8080/ps/xiaoming即可。