AI Hints
Home›Spring Boot Tutorial›API Versioning, CORS and Practice: A Complete Books API
Spring Boot · Lesson 90 of 120

Practice: A Complete Books API

Your turn.

⏱ 2 min read▶ Video at 1:59:55🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Let's finish our layered API. The service is ready. We'll add the last two endpoints to the controller.

  • The service is ready
  • Two endpoints left
  • Finish the API

The service

One finds a book, maps it to a response, or throws not found. Delete checks the id exists, then deletes it.

BookService.javajava
@Service
@Transactional
public class BookService {

    private final BookRepository books;

    public BookService(BookRepository books) { this.books = books; }

    @Transactional(readOnly = true)
    public BookResponse one(long id) {
        return books.findById(id).map(BookResponse::from)
                    .orElseThrow(() -> new BookNotFoundException(id));
    }

    public void delete(long id) {
        if (!books.existsById(id)) throw new BookNotFoundException(id);
        books.deleteById(id);
    }
}

GET and DELETE

Your turn. Pause the video, and try this. Add GET slash books slash id, and DELETE slash books slash id, to the controller. Delete should answer two oh four. Missing books are handled, by our global error handler. Here is one solution. Each method is just one line, calling the service. Book seven is found. Deleting it gives two oh four. And then it's gone, so we get a four oh four problem detail.

✍️ Exercise: GET and DELETE

BookController: GET /books/{id} → service.one(id); DELETE /books/{id} → service.delete(id) with 204 No Content.

Show hint

@ResponseStatus(HttpStatus.NO_CONTENT) on the delete method

Show solution
BookController.javajava
@RestController
@RequestMapping("/books")
public class BookController {

    private final BookService service;

    public BookController(BookService service) { this.service = service; }

    @GetMapping("/{id}")
    public BookResponse one(@PathVariable long id) {
        return service.one(id);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable long id) {
        service.delete(id);
    }
}
Output
$ curl localhost:8080/books/7
{"id":7,"title":"Emma","author":"Jane Austen","price":6.99}
$ curl -i -X DELETE localhost:8080/books/7 | head -1
HTTP/1.1 204
$ curl localhost:8080/books/7
{"title":"Not Found","status":404,"detail":"Book 7 was not found"}

What module ten taught

Split apps into controller, service, and repository. Use request and response DTOs. Update with dirty checking, and PATCH for partial changes. Offer filters, and pages, through query parameters. And version your API, and set up CORS, for real clients.

keyvalue
Layerscontroller, service, repository
DTOsCreateBook in, BookResponse out
Listsfilters, Pageable
Clientsversions, CORS

Summary

You can now design a clean, professional REST API. That's the end of module ten. Next module, security.

Key takeaways
  • Layers
  • DTOs
  • Paging and versions
Next up: Spring Security Basics — who are you?