@@ -0,0 +1,53 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
public class Student {
|
||||
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Student() {}
|
||||
|
||||
public Student(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Student(long id, String firstName, String lastName) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import com.baeldung.web.controller.students.StudentService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/students")
|
||||
public class StudentController {
|
||||
|
||||
@Autowired
|
||||
private StudentService service;
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Student> read() {
|
||||
return service.readAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Student> read(@PathVariable("id") Long id) {
|
||||
Student foundStudent = service.read(id);
|
||||
if (foundStudent == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} else {
|
||||
return ResponseEntity.ok(foundStudent);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public ResponseEntity<Student> create(@RequestBody Student student) throws URISyntaxException {
|
||||
Student createdStudent = service.create(student);
|
||||
|
||||
URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
|
||||
.path("/{id}")
|
||||
.buildAndExpand(createdStudent.getId())
|
||||
.toUri();
|
||||
|
||||
return ResponseEntity.created(uri)
|
||||
.body(createdStudent);
|
||||
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Student> update(@RequestBody Student student, @PathVariable Long id) {
|
||||
Student updatedStudent = service.update(id, student);
|
||||
if (updatedStudent == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
} else {
|
||||
return ResponseEntity.ok(updatedStudent);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Object> deleteStudent(@PathVariable Long id) {
|
||||
service.delete(id);
|
||||
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.web.controller.students;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StudentService {
|
||||
|
||||
// DB repository mock
|
||||
private Map<Long, Student> repository = Arrays.asList(
|
||||
new Student[]{
|
||||
new Student(1, "Alan","Turing"),
|
||||
new Student(2, "Sebastian","Bach"),
|
||||
new Student(3, "Pablo","Picasso"),
|
||||
}).stream()
|
||||
.collect(Collectors.toConcurrentMap(s -> s.getId(), Function.identity()));
|
||||
|
||||
// DB id sequence mock
|
||||
private AtomicLong sequence = new AtomicLong(3);
|
||||
|
||||
public List<Student> readAll() {
|
||||
return repository.values().stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Student read(Long id) {
|
||||
return repository.get(id);
|
||||
}
|
||||
|
||||
public Student create(Student student) {
|
||||
long key = sequence.incrementAndGet();
|
||||
student.setId(key);
|
||||
repository.put(key, student);
|
||||
return student;
|
||||
}
|
||||
|
||||
public Student update(Long id, Student student) {
|
||||
student.setId(id);
|
||||
Student oldStudent = repository.replace(id, student);
|
||||
return oldStudent == null ? null : student;
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
repository.remove(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user