-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/demo/lesson20/entity/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/demo/lesson20/service/StudentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/main/java/com/example/demo/lesson20/web/StudentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |