Book Store Application With Spring Boot And Ionic Part 1

Getting Started | Building an Application with Spring Boot




Build an application gyms with Spring boot and Angular use Jpa

Objective:

Book Store Application In this tutorial we will create a project with Spring Boot for backend and Ionic for frontend using Jpa for database. The application has Admin et User The Admin can add a category and book also he can update and delete all. The user can surfing the application find all the boos by category or search by name and show more details.


Used Skills in this project:

Java, SpringBoot, MicroServices, REST WebServices, JPA, Hibernate, OrikaBeanMapper, MySQL Database, Maven, Ionic, GitHub, SpringBoot Embedded Tomcat Server, STS-Eclipse IDE


Tools to be used

° Use any IDE to develop the Spring and Hibernate project. It may be STS/Eclipse/Netbeans. Here, we are using STS (Spring Tool Suite).

° Mysql for the database.

° Use any IDE to develop the Angular project. It may be Visual Studio Code/Sublime. Here, we are using Visual Studio Code.

° Server: Apache Tomcat/JBoss/Glassfish/Weblogic/Websphere.


Spring MVC

Spring MVC is the primary web framework built on the Servlet API. It is build on the popular MVC design pattern. MVC (Model-View-Controller) is a software architecture pattern, which separates application into three areas: model, view, and controller. The model represents a Java object carrying data. The view represents the visualization of the data that the model contains. The controller controls the data flow into model object and updates the view when the data changes. It separates the view and model.


Spring Boot Architecture



Spring Boot is a module of the Spring Framework. It is used to create stand-alone, production-grade Spring Based Applications with minimum efforts. It is developed on top of the core Spring Framework.



Introduction mysql to database

MySQL is a database management system that allows you to manage relational databases. It is open source software backed by Oracle. It means you can use MySQL without paying a dime. Also, if you want, you can change its source code to suit your needs. MySQL can run on various platforms UNIX, Linux, Windows, etc. You can install it on a server or even in a desktop. Besides, MySQL is reliable, scalable, and fast.



Introduction to the POM

POM A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/main/java; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

pom.xml

    
  
  
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0>/modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> </parent> <groupId>com.book..store</groupId> <artifactId>book-store</artifactId> <version>0.0.1-SNAPSHOT</version> <name>book-store</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Properties File

Properties files are used to keep ‘N’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application.properties file under the classpath.

The application.properties file is located in the src/main/resources directory. The code for sample application.properties


We need to create database name it book-store

src/main/resources/application.properties

    
  
spring.datasource.url=jdbc:mysql://localhost:3306/book-store?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.activemq.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.ddl-auto=create-drop server.port=8080

Class Diagram



Modals / Entities

An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.


We need to create three entities

- Book

- Category

- User


Here, we are creating an Entity

Book.java This object content id, name, photo, description, publishDate, writer, price, langue, page, size, publishDate, and relational @ManyToOne with Category

src/main/java/com//book/store/model/Book.java):



package com.book.store.modal; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Temporal; import javax.persistence.TemporalType; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; private String photo; private String description; @Temporal(TemporalType.TIMESTAMP) private Date publishDate; private String writer; private double price; private String langue; private String publisher; private int page; private float size; @ManyToOne @JsonBackReference(value = "category") private Category category; public Book() { super(); } public Book(String name, String photo, String description, Date publishDate, String writer, double price, String langue, String publisher, int page, float size, Category category) { super(); this.name = name; this.photo = photo; this.description = description; this.publishDate = publishDate; this.writer = writer; this.price = price; this.langue = langue; this.publisher = publisher; this.page = page; this.size = size; this.category = category; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Date getPublishDate() { return publishDate; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getLangue() { return langue; } public void setLangue(String langue) { this.langue = langue; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public float getSize() { return size; } public void setSize(float size) { this.size = size; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }

Here, we are creating an Entity

Category.java This object content id, name, expanded, and relational @ManyToOne with User and @OneToMany with Book

src/main/java/com//book/store/model/Category.java):



package com.book.store.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity public class Category { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; private boolean expanded; @OneToMany(cascade = CascadeType.ALL, mappedBy = "category") private List<Book> books; @ManyToOne @JsonBackReference(value = "user") private User user; public Category() { super(); } public Category(String name, boolean expanded, List<Book> books, User user) { super(); this.name = name; this.expanded = expanded; this.books = books; this.user = user; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isExpanded() { return expanded; } public void setExpanded(boolean expanded) { this.expanded = expanded; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void addBookToCategory(Book book) { if (getBooks() == null) { this.books = new ArrayList<>(); } getBooks().add(book); book.setCategory(this); } }

Here, we are creating an Entity

User.java This object content id, username, password, admin and relational @OneToMany with Category

src/main/java/com//book/store/model/User.java):



package com.book.store.modal; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(unique = true) private String username; private String password; private boolean admin; @OneToMany(cascade = CascadeType.ALL, mappedBy = "user") private List<Category> categories; public User() { super(); } public User(String username, String password, boolean admin, List<Category> categories) { super(); this.username = username; this.password = password; this.admin = admin; this.categories = categories; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isAdmin() { return admin; } public void setAdmin(boolean admin) { this.admin = admin; } public List<Category> getCategories() { return categories; } public void setCategories(List<Category> categories) { this.categories = categories; } public void addCategoryToUser(Category category) { if (getCategories() == null) { this.categories = new ArrayList<>(); } getCategories().add(category); category.setUser(this); } }

JPA Repositories

The JPA module of Spring Data contains a custom namespace that allows defining repository beans. It also contains certain features and element attributes that are special to JPA. Generally, the JPA repositories can be set up by using the repositories element, as shown in the following example:


Spring Repositories Spring has supported the concept of a repository for some time now. Repository is one of Spring's core stereotypes and you should plan on using them in your data access layer, regardless of your chosen data access layer API and framework.


We need to create three interfaces dao BookDao, We need one method findByName

- BookDao

- CategoryDao

- UserDao


Here, we are creating an interface

src/main/java/com/book/store/dao/BookDao.java):



package com.book.store.dao; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.book.store.modal.Book; public interface BookDao extends JpaRepository<Book, Long> { Optional<Book> findByName(String name); }

Here, we are creating an interface CategoryDao

src/main/java/com/book/store/dao/CategoryDao.java):



package com.book.store.dao; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.book.store.modal.Category; public interface CategoryDao extends JpaRepository<Category, Long> { }

Here, we are creating an interface BookDao, We need one method findByUsername

src/main/java/com/book/store/dao/UserDao.java):



package com.book.store.dao; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.book.store.modal.User; public interface UserDao extends JpaRepository<User, Long> { Optional<User> findByUsername(String name); }

The services

Service Components are the class file which contains @Service annotation. These class files are used to write business logic in a different layer, separated from @RestController class file. The logic for creating a service component class file is shown here


We will create two interfaces in service package:

- BookService.java
- CategoryService.java
- UserService.java

What's the difference between @Component, @Repository and @Service

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?


In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same way?


Here, we are creating a service interface BookService and creating Seven methods addBookToCategory, editBook, deleteBook, findBookByName, findBookById, findBooksForCategory and findllBooks

BookService.java This interface content seven methds addUser and editUser

src/main/java/com//book/store/service/BookService.java):



package com.book.store.service; import java.util.List; import com.book.store.modal.Book; public interface BookService { Book addBookToCategory(Book book, long id); Book editBook(Book book, long id); void deleteBook(long id); Book findBookByName(String name); Book findBookById(long id); List<Book> findllBooks(); List<Book> findBooksForCategory(long id); }

Here, we are creating a service interface CategoryService and creating Six methods addCategoryToUser, editCategory, deleteCategory, findBookByName, findCategoryById, findAllCategories and findCategoriesForUser

src/main/java/com//book/store/service/CategoryService.java):



package com.book.store.service; import java.util.List; import com.book.store.modal.Category; public interface CategoryService { Category addCategoryToUser(Category category, long id); Category editCategory(Category category, long id); void deleteCategory(long id); Category findCategoryById(long id); List<Category> findAllCategories(); List<Category> findCategoriesForUser(long id); }

Here, we are creating a service interface UserService and creating Four methods addUser, editUser, deleteUser, findBookByName and findByUsername

src/main/java/com//book/store/service/UserService.java):



package com.book.store.service; import com.book.store.modal.User; public interface UserService { User addUser(User user); User editUser(User user, long id); void deleteUser(long id); User findByUsername(String username); }

The managers classes implementation

An impl class is usually a class that implements the behaviour described by one of these interfaces. While the internal implementation class, With transactions configured, we can now annotation a bean with @Transactional and @Service either at the class or method level. Then conversion Entity to DTO. This is stands for Data Transfer Object and is a simple Plain Old Java Object which contains class properties and getters and settings methods for accessing those properties.


We will create Three classes

- BookServieImpl
- CategoryServiceImpl - UserServiceImpl

Here, we are creating a class

BookServiceImpl.java In this class we will implement BookService and @Override All methods


src/main/java/com//book/store/impl/BookServiceImpl.java):



package com.book.store.impl; import java.util.Date; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.book.store.dao.BookDao; import com.book.store.dao.CategoryDao; import com.book.store.modal.Book; import com.book.store.modal.Category; import com.book.store.service.BookService; @Transactional @Component public class BookServiceImpl implements BookService { @Autowired private BookDao bookDao; @Autowired private CategoryDao categoryDao; @Override public Book addBookToCategory(Book book, long id) { Category category = categoryDao.findById(id).orElse(null); category.addBookToCategory(book); return bookDao.save(book); } @Override public Book editBook(Book book, long id, long idCategory) { Book existsBook = bookDao.findById(id).orElse(null); Category category = categoryDao.findById(idCategory).orElse(null); existsBook.setName(book.getName()); existsBook.setDescription(book.getDescription()); existsBook.setPrice(book.getPrice()); existsBook.setPhoto(book.getPhoto()); existsBook.setLangue(book.getLangue()); existsBook.setPublishDate(new Date()); existsBook.setSize(book.getSize()); existsBook.setPage(book.getPage()); existsBook.setWriter(book.getWriter()); existsBook.setPublisher(book.getPublisher()); category.addBookToCategory(existsBook); return bookDao.save(existsBook); } @Override public void deleteBook(long id) { bookDao.deleteById(id); } @Override public Book findBookByName(String name) { Optional<Book> books = bookDao.findByName(name); if (books.isPresent()) { Book book = books.get(); return book; } return null; } @Override public Book findBookById(long id) { return bookDao.findById(id).orElse(null); } @Override public List<Book> findllBooks() { return bookDao.findAll(); } @Override public List<Book> findBooksForCategory(long id) { Category category = categoryDao.findById(id).orElse(null); return category.getBooks(); } }

Here, we are creating a class

BookServiceImpl.java In this class we will implement CategoryService and @Override All methods


src/main/java/com//book/store/impl/CategoryServiceImpl.java):



package com.book.store.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.book.store.dao.CategoryDao; import com.book.store.dao.UserDao; import com.book.store.modal.Category; import com.book.store.modal.User; import com.book.store.service.CategoryService; @Transactional @Component public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryDao categoryDao; @Autowired private UserDao userDao; @Override public Category addCategoryToUser(Category category, long id) { User user = userDao.findById(id).orElse(null); List<Category> categories = categoryDao.findAll(); if(categories.size()==0) { category.setExpanded(true); } else { category.setExpanded(false); } user.addCategoryToUser(category); return categoryDao.save(category); } @Override public Category editCategory(Category category, long id, long idUser) { Category existCategory = categoryDao.findById(id).orElse(null); User user = userDao.findById(idUser).orElse(null); existCategory.setName(category.getName()); user.addCategoryToUser(existCategory); return categoryDao.save(existCategory); } @Override public void deleteCategory(long id) { categoryDao.deleteById(id); } @Override public Category findCategoryById(long id) { return categoryDao.findById(id).orElse(null); } @Override public List<Category> findAllCategories() { return categoryDao.findAll(); } @Override public List<Category> findCategoriesForUser(long id) { User user = userDao.findById(id).orElse(null); return user.getCategories(); } }

Here, we are creating a class

BookServiceImpl.java In this class we will implement UserService and @Override All methods


src/main/java/com//book/store/service/UserServiceImpl.java):



package com.book.store.impl; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import com.book.store.dao.UserDao; import com.book.store.modal.User; import com.book.store.service.UserService; @Transactional @Component public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public User addUser(User user) { List<User> users = userDao.findAll(); if (users.size() == 0) { user.setAdmin(true); } else { user.setAdmin(false); } return userDao.save(user); } @Override public User editUser(User user, long id) { User userExistsUser = userDao.findById(id).orElse(null); userExistsUser.setAdmin(users.isAdmin()); userExistsUser.setUsername(users.getUsername()); userExistsUser.setPassword(users.getPassword()); return userDao.save(userExistsUser); } @Override public void deleteUser(long id) { userDao.deleteById(id); } @Override public User findByUsername(String username) { Optional<User> users = userDao.findByUsername(username); if (users.isPresent()) { User user = users.get(); return user; } return null; } }

@RestController API

@RestController is a convenience annotation for creating Restful controllers. It is a specialization of @Component and is autodetected through classpath scanning. It adds the @Controller and @ResponseBody annotations. It converts the response to JSON or XML. It does not work with the view technology, so the methods cannot return ModelAndView. It is typically used in combination with annotated handler methods based on the @RequestMapping annotation.


We will create three classes in controller package:

- BookController
- CategoryController
- UserController

Here, we are creating a controller class

BookController.java We'll inject this bean into the ShoppingManagerService bean using @Autowired on the field definition: We need to add map requests with request mapping annotations e.g. @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

src/main/java/com//book/store/controller/BookController.java):



package com.book.store.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.book.store.modal.Book; import com.book.store.service.BookService; @RestController @RequestMapping("api") @CrossOrigin(origins = "*") public class BookController { @Autowired private BookService bookService; @PostMapping("/addBookToCategory/{id}") Book addBookToCategory(@RequestBody Book book, @PathVariable long id) { return bookService.addBookToCategory(book, id); } @PutMapping("/editBook/{id}/{idCategory}") Book editBook(@RequestBody Book book, @PathVariable long id, @PathVariable long idCategory) { return bookService.editBook(book, id, idCategory); } @DeleteMapping("/deleteBook/{id}") void deleteBook(@PathVariable long id) { bookService.deleteBook(id); } @GetMapping("/findBookByName/{name}") Book findBookByName(@PathVariable String name) { return bookService.findBookByName(name); } @GetMapping("/findBookById/{id}") Book findBookById(@PathVariable long id) { return bookService.findBookById(id); } @GetMapping("/findllBooks") List<Book> findllBooks() { return bookService.findllBooks(); } @GetMapping("/findBooksForCategory/{id}") List<Book> findBooksForCategory(@PathVariable long id) { return bookService.findBooksForCategory(id); } }

Here, we are creating a controller class

CategoryController.java We'll inject this bean into the ShoppingManagerService bean using @Autowired on the field definition: We need to add map requests with request mapping annotations e.g. @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

src/main/java/com//book/store/controller/CategoryController.java):



package com.book.store.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.book.store.modal.Category; import com.book.store.service.CategoryService; @RestController @RequestMapping("api") @CrossOrigin(origins = "*") public class CategoryController { @Autowired private CategoryService categoryService; @PostMapping("/addCategoryToUser/{id}") Category addCategoryToUser(@RequestBody Category category, @PathVariable long id) { return categoryService.addCategoryToUser(category, id); } @PutMapping("/editCategory/{id}/{idUser}") Category editCategory(@RequestBody Category category, @PathVariable long id, @PathVariable long idUser) { return categoryService.editCategory(category, id, idUser); } @DeleteMapping("/deleteCategory/{id}") void deleteCategory(@PathVariable long id) { categoryService.deleteCategory(id); } @GetMapping("/findCategoryById/{id}") Category findCategoryById(@PathVariable long id) { return categoryService.findCategoryById(id); } @GetMapping("/findAllCategories") List<Category> findAllCategories() { return categoryService.findAllCategories(); } @GetMapping("/findCategoriesForUser/{id}") List<Category> findCategoriesForUser(@PathVariable long id) { return categoryService.findCategoriesForUser(id); } }

Here, we are creating a controller class

UserController.java We'll inject this bean into the ShoppingManagerService bean using @Autowired on the field definition: We need to add map requests with request mapping annotations e.g. @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

src/main/java/com//book/store/controller/UserController.java):



package com.book.store.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.book.store.modal.User; import com.book.store.service.UserService; @RestController @RequestMapping("api") @CrossOrigin(origins = "*") public class UserController { @Autowired private UserService userService; @PostMapping("/addUser") User addUser(@RequestBody User user) { return userService.addUser(user); } @PutMapping("/editUser/{id}") User editUser(@RequestBody User user, @PathVariable long id) { return userService.editUser(user, id); } @DeleteMapping("/deleteUser/{id}") void deleteUser(@PathVariable long id) { userService.deleteUser(id); } @GetMapping("/findByUsername/{username}") User findByUsername(@PathVariable String username) { return userService.findByUsername(username); } }

BookStoreApplication

@BookStoreApplication and its use in a simple Spring Boot application. We use the @SpringBootApplication annotation in our Application or Main class to enable a host of features, e.g. Java-based Spring configuration, component scanning, and in particular for enabling Spring


src/main/java/com/book/store/BookStoreApplication.java):



package com.shoppingapp; import org.springframework.context.annotation.ComponentScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @ComponentScan @SpringBootApplication public class BookStoreApplication { public static void main(String[] args) { SpringApplication.run(BookStoreApplication.class, args); System.out.println("App started...."); } }


Conclusion

Now we have an overview of Spring Boot CRUD example when building a CRUD App.

We also take a look at client-server architecture for REST API using Spring Web MVC & Spring Data JPA, as well, we are gooing to continue with Angular 10 project structure for building a front-end app to make HTTP requests and consume responses.




Post a Comment

Previous Post Next Post


Follow us on facebook