My First Full Stack Spring Boot / JQuery Application
Part 4 - Creating a Controller
Create a Controller
- Controllers define how to handle incoming requests from and outgoing responses to a client.
- Controllers provides all of the necessary endpoints to access and manipulate respective domain objects.
- REST resources are identified using URI endpoints.
- The aggregate of all controller endpoints exposed by a webserver is known as the webserver’s web API.
@Controller
public class PersonController {
private PersonService service;
@Autowired
public PersonController(PersonService service) {
this.service = service;
}
@PostMapping(value = "/create")
public ResponseEntity<Person> create(@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}
@GetMapping(value = "/read/{id}")
public ResponseEntity<Person> readById(@PathVariable Long id) {
return new ResponseEntity<>(service.readById(id), HttpStatus.OK);
}
@GetMapping(value = "/readAll")
public ResponseEntity<List<Person>> readAll() {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}
@PutMapping(value = "/update/{id}")
public ResponseEntity<Person> updateById(
@PathVariable Long id,
@RequestBody Person newData) {
return new ResponseEntity<>(service.update(id, newData), HttpStatus.OK);
}
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Person> deleteById(@PathVariable Long id) {
return new ResponseEntity<>(service.deleteById(id), HttpStatus.OK);
}
}
constructor
create Method
- The
POSTverb functionality in acreatemethod allows us to add a newPersonrecord. - Take note that the method
- has a parameter of type
@RequestBody Person person@RequestBodytells Spring that the entire request body needs to be converted to an instance ofPerson
- delegates the
Personpersistence toPersonRepository’s save method called by thePersonService’screatemethod.
- has a parameter of type
@PostMapping(value = "/create")
public ResponseEntity<Person> create(@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}
readById Method
- The code snippet below allows us to access an individual
Person. - The value attribute in
@GetMappingtakes a URI template/read/{id}. - The placeholder
{id}along with@PathVaribleannotation allows Spring to examine the request URI path and extract thepollIdparameter value. - Inside the method, we use the
PersonService’sreadByIdfinder method to retrieve the respectivePersonfrom thePersonRepositoryand pass the result to aResponseEntity.
@GetMapping(value = "/read/{id}")
public ResponseEntity<Person> readById(@PathVariable Long id) {
return new ResponseEntity<>(service.readById(id), HttpStatus.OK);
}
readAll Method
- The code snippet below allows us to access all
Personrecords. - The value attribute in
@GetMappingtakes a URI template/read/{id}. - The placeholder
{id}along with@PathVaribleannotation allows Spring to examine the request URI path and extract thepollIdparameter value. - Inside the method, we use the
PersonService’sreadByIdfinder method to retrieve the respectivePersonfrom thePersonRepositoryand pass the result to aResponseEntity.
@GetMapping(value = "/readAll")
public ResponseEntity<List<Person>> readAll() {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}
update Method
- The code snippet below enables us to update a
Personwith new data.
@PutMapping(value = "/update/{id}")
public ResponseEntity<Person> updateById(
@PathVariable Long id,
@RequestBody Person newData) {
return new ResponseEntity<>(service.update(id, newData), HttpStatus.OK);
}
deleteById Method
- The code snippet below enables us to delete a
Person
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Person> deleteById(@PathVariable Long id) {
return new ResponseEntity<>(service.deleteById(id), HttpStatus.OK);
}





