Skip to content

Commit

Permalink
Add student service
Browse files Browse the repository at this point in the history
  • Loading branch information
cj96248 committed Dec 18, 2018
1 parent 653a10b commit a093308
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/com/example/demo/lesson20/entity/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.demo.lesson20.entity;

public class Student {

private String id;
private String name;
private Integer age;

public Student() {
}
public Student(String id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.demo.lesson20.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.example.demo.lesson20.entity.Student;

@Service
public class StudentService {

static Map<String, Student> students = new HashMap<>();

static {
students.put("11", new Student("11", "Tom", 23));
students.put("12", new Student("12", "Jerry", 25));
students.put("13", new Student("13", "David", 32));
students.put("14", new Student("14", "Jack", 41));
}

public Student findById(String id) {
return students.get(id);
}

public List<Student> findAll() {
return new ArrayList<Student>(students.values());
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/example/demo/lesson20/web/StudentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.demo.lesson20.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.lesson20.entity.Student;
import com.example.demo.lesson20.service.StudentService;

@RestController
@RequestMapping("/student")
public class StudentController {

@Autowired
private StudentService studentService;

@GetMapping(value="/id1")
public Student findById(String id) {
return studentService.findById(id);
}

@GetMapping(value="/id2", produces=MediaType.APPLICATION_XML_VALUE)
public Student findById2(String id) {
return studentService.findById(id);
}

@GetMapping(value="/list1")
public List<Student> findAll(){
return studentService.findAll();
}

@GetMapping(value="/list2", produces=MediaType.APPLICATION_JSON_VALUE)
public List<Student> findAll2(){

return studentService.findAll();
}
}

0 comments on commit a093308

Please sign in to comment.