Coder Social home page Coder Social logo

ssaleem / cheese-rest-api Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 118 KB

A Spring Boot REST API to perform CRUD operations on Cheeses, Cheese Categories and Menus

Home Page: https://cheesemvc-api.herokuapp.com/swagger-ui.html#

Java 100.00%
rest-api spring-boot swagger-ui spring-data-jpa hibernate-orm model-mapper data-transfer-object herokuapp cleardb many-to-many one-to-many

cheese-rest-api's Introduction

Cheeses - A REST API

REST API snapshot

Contents

Features

Resources and Operations

This REST API handles three types of resources.

Resource Attributes Create Read Update Delete
Cheese Name, Description, Rating, Cheese Category
Cheese Category Name
Menu Name, Set<Cheese>

Model Validation

Attributes in all the entities are annotated using Java Validation API annotations to provide automatic validation and error feedback.

Data Transfer Objects and ModelMapper

  • For cheese entity, a data transfer object CheeseDto is used to send and receive relevant attributes efficiently to/from the API consumer. Upon receiving CheeseDto, modelMapper.map() method is used to convert data transfer object to cheese entity.
  • For category entity, @JsonIgnore is used to exclude serialization of list of cheeses that have the same category when sending category data.

Data Access Objects(DAO)

  • Spring Data repository abstraction significantly reduces the amount of boilerplate code required to implement data access layers for various persistence stores. Spring Data CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed. DAO's for cheese, cheese category and menu are created through CrudRepository interface.
  • Spring Data JPA @Query annotation is used to declare custom queries for entities managed by DAO.

Resource Relationship Handling

  • Cheese Category-Cheese(many to one): Multiple cheeses can have same category. @OneToMany and @ManyToOne annotations from JPA are used to define a many-to-one relationship between the join tables.

  • Cheese-Menu(many to many): A menu can have multiple cheeses and a cheese. can belong to multiple menus. @ManyToMany annotation from JPA is used to define a many-to-many relationship between the join tables.

    • Removing entities with bidirectional association: Using @PreRemove entity lifecycle hook, a callback is provided to remove all the menu associations for a particular cheese . Callback+ annotated with @PreRemove is executed before the entity manager's remove operation is actually executed or cascaded

    +: Callback methods are user defined methods that are attached to entity lifecycle events through annotations and are invoked automatically by JPA when these events occur.

Exception Handling

  • A generic ResourceNotFoundException is added to send custom error message to API consumer as a result of accessing un-available resources.
  • For category entity, category name column must be unique and this constraint is enforced by setting uniqueConstraints attribute on category table. A controller level exception handler is used in category resource to handle exceptions caused by adding a category name that already exists . This handler generates appropriate validation error messages as a result of @UniqueConstraint violation.

Visual Documentation

For documentation OpenAPI/Swagger 2 specification annotations are used. Springfox implementation of Swagger 2, along with Swagger UI libraries are used to generate interactive documentation.

Built with

  • Spring Boot - An opinionated view of the Spring platform with 'starter' dependencies to simplify build configuration and create stand-alone Spring applications
  • Spring Initializr - A web based tool to quickly generate Spring Boot projects.
  • Java Persistence API - Java Persistence API is a source to store business entities as relational entities. It shows how to define a Plain Old Java Object (POJO) as an entity and how to manage entities with relations.
  • Java Validation API Annotations - The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean.
  • Spring Boot Data JPA Starter - Starter for using Spring Data JPA with Hibernate
  • Hibernate ORM - An object-relational mapping tool for the Java programming language. Hibernate is an implementation of the Java Persistence API (JPA) specification. It provides a framework for mapping an object-oriented domain model to a relational database.
  • ModelMapper - An object mapping library that uses conventions to determine how one object model maps to another.
  • SpringFox - A project that aims at creating automated JSON API documentation for API's built with Spring.
  • Swagger UI - An open source tool for visual documentation of REST APIs. It allows anyone to visualize and interact with the API’s resources without having any of the implementation logic in place.

Live Version

The live version of this API can be explored at either of the following links.

cheese-rest-api's People

Contributors

ssaleem avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

danny-hoang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.