Spring Boot Restfu
1.创建maven项目
新建项目并引进jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath />
</parent>
<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>
2.创建实体:
package RestfulDemo.RestfulDemo;
public class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
public class Students {
private List<Student> studentList;
public List<Student> getStudentList() {
if(studentList == null) {
studentList = new ArrayList<>();
}
return studentList;
}
public void setEmployeeList(List<Student> studentList) {
this.studentList = studentList;
}
}
3.模拟DAO层:
@Repository
public class StudentDao {
private static Students list = new Students();
static
{
list.getStudentList().add(new Student("001", "test1"));
list.getStudentList().add(new Student("002", "test2"));
list.getStudentList().add(new Student("003", "test3"));
}
public Students getAllStudents()
{
return list;
}
public void addStudent(Student student) {
list.getStudentList().add(student);
}
}
4.编写Spring boot 启动类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}