/RestAPIwithSpringMVC

RestAPI with Spring MVC

Primary LanguageJava

Rest API with SpringMVC

Using technologies

  • Spring Boot & MVC
  • H2 Embedded DB
  • JPA/Hibernate
  • JUnit
  • Maven

REST API User Endpoints

HTTP Method Request URI Request Body Response Body HTTP Status
GET /rest/users - List < User > 200/500
GET /rest/users?ln=surname - List < User > 200/500
GET /rest/users/{id} - User 200/404/500
POST /rest/users User URI 201/500
PUT /rest/users/{id} User - 200/404/500
DELETE /rest/users/{id} - - 200/404/500

Examples

This Get request returns all users

GET http://localhost:8080/rest/users

Here’s the JSON response

[
    {
        "id": 1,
        "firstName": "Namiq",
        "lastName": "Bayramov",
        "age": 25
    },
    {
        "id": 2,
        "firstName": "Azər",
        "lastName": "Ağayev",
        "age": 21
    },
    {
        "id": 3,
        "firstName": "Ali",
        "lastName": "Aliyev",
        "age": 35
    },
    {
        "id": 4,
        "firstName": "Güney",
        "lastName": "Çakır",
        "age": 22
    }
]

This Get request returns the user that matches the id path variable

GET http://localhost:8080/rest/users/1

Here’s the JSON response

{
    "id": 1,
    "firstName": "Namiq",
    "lastName": "Bayramov",
    "age": 25
}

This Get request returns the users that matches the ln request parameter

GET http://localhost:8080/rest/users?ln=Aliyev

Here’s the JSON response

[
    {
        "id": 3,
        "firstName": "Ali",
        "lastName": "Aliyev",
        "age": 35
    }
]

This Post request creates a new User object that corresponds to the User context sent in the request body

POST http://localhost:8080/rest/users

Here’s the JSON request body

{
    "id": 7,
    "firstName": "NewUser",
    "lastName": "Test",
    "age": 22
}

Here’s the response header

location     http://localhost:8080/rest/users/7

User content sent by this Put request updates the User object that matches the id value in the Database.

PUT http://localhost:8080/rest/users/1

Here’s the JSON request body

{
    "firstName": "UpdateFirstName",
    "lastName": "UpdateLastName",
    "age": 33
}

This Delete request delete the User object that matches the id path variable

DELETE http://localhost:8080/rest/users/1

Visit my blog kiberbloq.com