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
POST
verb functionality in acreate
method allows us to add a newPerson
record. - Take note that the method
- has a parameter of type
@RequestBody Person person
@RequestBody
tells Spring that the entire request body needs to be converted to an instance ofPerson
- delegates the
Person
persistence toPersonRepository
’s save method called by thePersonService
’screate
method.
- 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
@GetMapping
takes a URI template/read/{id}
. - The placeholder
{id}
along with@PathVarible
annotation allows Spring to examine the request URI path and extract thepollId
parameter value. - Inside the method, we use the
PersonService
’sreadById
finder method to retrieve the respectivePerson
from thePersonRepository
and 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
Person
records. - The value attribute in
@GetMapping
takes a URI template/read/{id}
. - The placeholder
{id}
along with@PathVarible
annotation allows Spring to examine the request URI path and extract thepollId
parameter value. - Inside the method, we use the
PersonService
’sreadById
finder method to retrieve the respectivePerson
from thePersonRepository
and 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
Person
with 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);
}