My First Full Stack Spring Boot / JQuery Application

Part 4 - Creating a Controller

Create a Controller

@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
@PostMapping(value = "/create")
public ResponseEntity<Person> create(@RequestBody Person person) {
    return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}

readById Method
@GetMapping(value = "/read/{id}")
public ResponseEntity<Person> readById(@PathVariable Long id) {
    return new ResponseEntity<>(service.readById(id), HttpStatus.OK);
}

readAll Method
@GetMapping(value = "/readAll")
public ResponseEntity<List<Person>> readAll() {
    return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

update Method
@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
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Person> deleteById(@PathVariable Long id) {
    return new ResponseEntity<>(service.deleteById(id), HttpStatus.OK);
}