Giter Club home page Giter Club logo

spring-restfulapiswith-hateos-hal's Introduction

Spring-RESTfulApisWith-HATEOS-HAL

This is a hobby project(6) to learn Java-Spring Boot and RESTful-API's conforming to the standards of HATEOS by creating Payroll+Orders app.

I built REST API's using Spring MVC + Spring HATEOAS app with HAL representations of each resource. This project is based on Building REST services with Spring course

Whats used?

Java + Spring Boot

Objectives

Create a nonREST API's — Simple Spring MVC app with no hypermedia

Make the API's RESTful — Spring MVC + Spring HATEOAS app with HAL representations of each resource

Introduce backward compactability — REST app where a field is evolved but old data is retained for backward compatibility

Add conditional link in responses. — REST app where conditional links are used to signal valid state changes to clients

How to run the program?

$ git clone https://github.com/rasi5050/Spring-RESTfulApisWith-HATEOS-HAL

$ cd Spring-RESTfulApisWith-HATEOS-HAL

$ ./mvnw clean spring-boot:run

Endpoints

General Error Response

{
    "timestamp": "2023-07-04T17:58:07.991+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/employees/76"
}

GET localhost:8080/employees

Success Response Body

{
    "_embedded": {
        "employeeList": [
            {
                "id": 1,
                "firstName": "Bilbo",
                "lastName": "Baggins",
                "role": "burglar",
                "name": "Bilbo Baggins",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/employees/1"
                    },
                    "employees": {
                        "href": "http://localhost:8080/employees"
                    }
                }
            },
            {
                "id": 2,
                "firstName": "Frodo",
                "lastName": "Baggins",
                "role": "thief",
                "name": "Frodo Baggins",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/employees/2"
                    },
                    "employees": {
                        "href": "http://localhost:8080/employees"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/employees"
        }
    }
}

GET localhost:8080/employees/1

Success Response Body

{
    "id": 1,
    "firstName": "Bilbo",
    "lastName": "Baggins",
    "role": "burglar",
    "name": "Bilbo Baggins",
    "_links": {
        "self": {
            "href": "http://localhost:8080/employees/1"
        },
        "employees": {
            "href": "http://localhost:8080/employees"
        }
    }
}

POST localhost:8080/employees

Request Body

{
    "name": "Alfred Hitchcock",
    "role": "filmmaker"
}

Success Response Body

{
    "id": 3,
    "firstName": "Alfred",
    "lastName": "Hitchcock",
    "role": "filmmaker",
    "name": "Alfred Hitchcock",
    "_links": {
        "self": {
            "href": "http://localhost:8080/employees/3"
        },
        "employees": {
            "href": "http://localhost:8080/employees"
        }
    }
}

PUT localhost:8080/employees/3

Request Body

{
    "name": "Samwise Gamgee",
    "role": "ring bearer"
}

Success Response Body

{
    "id": 3,
    "firstName": "Samwise",
    "lastName": "Gamgee",
    "role": "ring bearer",
    "name": "Samwise Gamgee",
    "_links": {
        "self": {
            "href": "http://localhost:8080/employees/3"
        },
        "employees": {
            "href": "http://localhost:8080/employees"
        }
    }
}

DELETE localhost:8080/employees/3

Success Response

204 No Content

GET localhost:8080/orders

Success Response Body

{
    "_embedded": {
        "orderList": [
            {
                "id": 1,
                "description": "MacBook Pro",
                "status": "COMPLETED",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/orders/1"
                    },
                    "orders": {
                        "href": "http://localhost:8080/orders"
                    }
                }
            },
            {
                "id": 2,
                "description": "iPhone",
                "status": "IN_PROGRESS",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/orders/2"
                    },
                    "orders": {
                        "href": "http://localhost:8080/orders"
                    },
                    "cancel": {
                        "href": "http://localhost:8080/orders/2/cancel"
                    },
                    "complete": {
                        "href": "http://localhost:8080/orders/2/complete"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/orders"
        }
    }
}

DELETE localhost:8080/orders/2/cancel

Success Response Body

{
    "id": 2,
    "description": "iPhone",
    "status": "CANCELLED",
    "_links": {
        "self": {
            "href": "http://localhost:8080/orders/2"
        },
        "orders": {
            "href": "http://localhost:8080/orders"
        }
    }
}

Failure Response Body

{
    "title": "Method not allowed",
    "detail": "You can't cancel an order that is in CANCELLED status"
}

PUT localhost:8080/orders/2/complete

Success Response Body

{
    "id": 2,
    "description": "iPhone",
    "status": "COMPLETED",
    "_links": {
        "self": {
            "href": "http://localhost:8080/orders/2"
        },
        "orders": {
            "href": "http://localhost:8080/orders"
        }
    }
}

Failure Response Body

{
    "title": "Method not allowed",
    "detail": "You cant complete an order that is in CANCELLED status"
}

Screenshots

Screenshot 2023-07-04 at 14 40 57 Screenshot 2023-07-04 at 14 43 40

Thanks!

spring-restfulapiswith-hateos-hal's People

Contributors

rasi5050 avatar

Watchers

 avatar

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.