Java Sample Project

Simple Spring boot crud application

  1. Create a new Spring Boot project using Spring Initializr. Include the following dependencies: Spring Web, Spring Data JPA, H2 Database, and Lombok.
  2. Create a new entity class for your application. This class will represent a model object that you will persist in the database. Here’s an example:
@Entity
@Table(name = "books")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @NotNull
    private String title;
 
    @NotNull
    private String author;
 
    @NotNull
    private int year;
}

3. Create a new repository interface for your entity. This interface will provide the basic CRUD operations for your entity. Here’s an example:

public interface BookRepository extends JpaRepository<Book, Long> {
}

4. Create a new controller class for your REST API. This class will handle HTTP requests and delegate the processing to the appropriate service. Here’s an example:

@RestController
@RequestMapping("/api/books")
public class BookController {
 
    @Autowired
    private BookService bookService;
 
    @GetMapping("")
    public List<Book> getAllBooks() {
        return bookService.getAllBooks();
    }
 
    @PostMapping("")
    public Book createBook(@RequestBody Book book) {
        return bookService.createBook(book);
    }
 
    @GetMapping("/{id}")
    public Book getBookById(@PathVariable Long id) {
        return bookService.getBookById(id);
    }
 
    @PutMapping("/{id}")
    public Book updateBook(@PathVariable Long id, @RequestBody Book book) {
        return bookService.updateBook(id, book);
    }
 
    @DeleteMapping("/{id}")
    public void deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
    }
}

5. Create a new service class for your business logic. This class will handle the data access and provide the necessary methods to the controller. Here’s an example:

@Service
public class BookService {
 
    @Autowired
    private BookRepository bookRepository;
 
    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }
 
    public Book createBook(Book book) {
        return bookRepository.save(book);
    }
 
    public Book getBookById(Long id) {
        return bookRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Book not found with id " + id));
    }
 
    public Book updateBook(Long id, Book book) {
        Book existingBook = getBookById(id);
        existingBook.setTitle(book.getTitle());
        existingBook.setAuthor(book.getAuthor());
        existingBook.setYear(book.getYear());
        return bookRepository.save(existingBook);
    }
 
    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}

6. Run your application and test the API using a tool like Postman or curl. Here are some sample requests:

GET /api/books
POST /api/books
{
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "year": 1925
}
GET /api/books/1
PUT /api/books/1
{
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "year": 1926
}
DELETE /api/books/1