View on GitHub

reads

https://yazansneneh.github.io/reads/

Read: 12 - Spring RESTful Routing & Static Files

Spring Request Mapping

@GetMapping("/{id}")
public ResponseEntity<?> getBazz(@PathVariable String id){
    return new ResponseEntity<>(new Bazz(id, "Bazz"+id), HttpStatus.OK);
}

@PostMapping
public ResponseEntity<?> newBazz(@RequestParam("name") String name){
    return new ResponseEntity<>(new Bazz("5", name), HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<?> updateBazz(
  @PathVariable String id,
  @RequestParam("name") String name) {
    return new ResponseEntity<>(new Bazz(id, name), HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<?> deleteBazz(@PathVariable String id){
    return new ResponseEntity<>(new Bazz(id), HttpStatus.OK);
}

Accessing Data with JPA

CrudRepository

Crud methods : save()

PagingAndSortingRepository

interface provides a method findAll(Pageable pageable), it help me implement Pagination. pageable object contain at least these 3 props: Page size, Current page number, Sorting.

JpaRepository

the interface props findAll() get a List of all available entities in database findAll() get a List of all available entities and sort them using the provided condition save() save an list of entities. Here, we can pass multiple objects to save them in a batch flush() flush all pending task to the database saveAndFlush() save the entity and flush changes immediately deleteInBatch() delete an Iterable of entities. Here, we can pass multiple objects to delete them in a batch