springboot快速入门
一、创建maven教程,搭建基本springboot环境的pom.xml文件如下:
<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.xujie</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- springboot启动器的父节点依赖,引入之后,后边不用指定springboot的版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/>
</parent>
<!--maven的编码格式,和jdk的版本 -->
<properties>
<!-- 让整个项目统一编码格式,该配置可以默认被识别 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 设置项目的jdk版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--maven插件,为springboott提供执行maven的支持 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、创建一个启动类,用于一键启动项目
这个启动类能够管辖的类包括与启动同包下的和子包下的所有,否则程序启动,不加载。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
三、一个Controller
@RestController //包括了@ResponseBody和Controller
public class UserController {
@GetMapping("/user")
public User findOne(@PathVariable Long id) {
User user = new User();
user.setAge("20");
user.setName("小明");
return user;
}
}
四、 pojo类
public class User {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}