Giter Club home page Giter Club logo

restapi's Introduction

REST API using Spring Boot and JWT

REST API written in Java using Spring Boot framework & Java Web Token for authorization.

HTTP endpoints

Method Endpoint
GET /api/v1/users
GET /api/v1/users/{publicId}
POST /api/v1/users
PUT /api/v1/users/{publicId}
PATCH /api/v1/users/{publicId}
DELETE /api/v1/users/{publicId}
POST /authenticate
GET /v2/api-docs

Usage

Project can be run from terminal using Maven wrapper

./mvnw spring-boot:run

We can freely access user data with public information (no password or internal ID provided)

GET /api/v1/users
GET /api/v1/users/{publicId}

To login(get JWT token) using user data, we must provide email & password to endpoint '/authenticate'

POST /authenticate

{
    "email":    "[email protected]",
    "password": "encryptedPassword"
}

In return we get JSON response with generated JWT token

{
    "jwt": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImV4cCI6MTYyMzM0NDY3OSwiaWF0IjoxNjIzMzA4Njc5fQ.K0F9vsNSvzd5uqOf6Y7ZvU54WUBZL5GNQ6wPVvU5S1E"
}

Providing this token in Authorization header with Bearer keyword used by default in Spring Security, we can access POST, PUT, PATCH, DELETE HTTP methods for '/users' endpoints.

Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImV4cCI6MTYyMzM0NDY3OSwiaWF0IjoxNjIzMzA4Njc5fQ.K0F9vsNSvzd5uqOf6Y7ZvU54WUBZL5GNQ6wPVvU5S1E

API Reference

We can access REST API interactive documentation created with Swagger 2 using endpoint:

GET /swagger-ui.html

Get all users

Request

GET /api/v1/users

Response

[
    {
        "publicId": "c928f4b3-38aa-4e7d-b0f3-948b8cd78153",
        "firstName": "John",
        "lastName": "Doe",
        "email": "[email protected]"
    },
    {
        "publicId": "c7fc5255-3d3c-4b41-bffe-e34c6f599e7b",
        "firstName": "Jane",
        "lastName": "Doe",
        "email": "jane.doe@@example.com"
    },
    {
        "publicId": "69e66da7-5a68-4f19-8e61-852bd3b2f793",
        "firstName": "Oscar",
        "lastName": "Dean",
        "email": "[email protected]"
    }
]

Get user

Request

GET /api/v1/users/${publicId}
Parameter Type Description
publicId UUID Required. Public id of user to fetch

Response

{
    "publicId": "c928f4b3-38aa-4e7d-b0f3-948b8cd78153",
    "firstName": "John",
    "lastName": "Doe",
    "email": "[email protected]"
}

Authenticate

Request

POST /authenticate

  {
    "email":    "[email protected]",
    "password": "encryptedPassword"
  }

Response

200 OK
Content-Type: application/json

  {
    "jwt": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImV4cCI6MTYyMzM0NDY3OSwiaWF0IjoxNjIzMzA4Njc5fQ.K0F9vsNSvzd5uqOf6Y7ZvU54WUBZL5GNQ6wPVvU5S1E"
  }

Create user

Request

POST /api/v1/users

  {
    "firstName":"Jane",
    "lastName":"Doe",
    "email":"[email protected]",
    "password":"SecurePassword"
  }
Field Type Description
firstName string Optional. User first name
lastName string Optional. User last name
email string Required. User email
password string Required. User password
Header Format Description
Authorization Bearer Required. JSON Web Token

Response

201 Created
Content-Type: application/json

  {
    "publicId": "0386e5ed-a92b-42d3-8015-5a124633b565",
    "firstName": "Jane",
    "lastName": "Doe",
    "email": "[email protected]"
  }

Update user

Request

PUT /api/v1/users/${publicId}

  {
    "firstName":"JaneUpdated",
    "lastName":"DoeUpdated",
    "email":"[email protected]",
    "password":"SecurePasswordUpdated"
  }
Parameter Type Description
publicId UUID Required. Public id of user to fetch
Field Type Description
firstName string Optional. User first name
lastName string Optional. User last name
email string Required. User email
password string Required. User password
Header Format Description
Authorization Bearer Required. JSON Web Token

Response

201 Created
Content-Type: application/json

  {
    "publicId": "0386e5ed-a92b-42d3-8015-5a124633b565",
    "firstName":"JaneUpdated",
    "lastName":"DoeUpdated",
    "email":"[email protected]"
  }

Partially update user

Request

PATCH /api/v1/users/${publicId}

  {
    "firstName":"JanePartialyUpdated",
    "lastName":"DoePartialyUpdated",
    "email":"[email protected]",
    "password":"SecurePasswordPartialyUpdated"
  }
Parameter Type Description
publicId UUID Required. Public id of user to fetch
Field Type Description
firstName string Optional. User first name
lastName string Optional. User last name
email string Optional. User email
password string Optional. User password
Header Format Description
Authorization Bearer Required. JSON Web Token

Response

201 Created
Content-Type: application/json

  {
    "publicId": "0386e5ed-a92b-42d3-8015-5a124633b565",
    "firstName":"JanePartialyUpdated",
    "lastName":"DoePartialyUpdated",
    "email":"[email protected]"
  }

Delete user

Request

DELETE /api/v1/users/${publicId}
Parameter Type Description
publicId UUID Required. Public id of user to fetch
Header Format Description
Authorization Bearer Required. JSON Web Token

Response

204 No Content

restapi's People

Contributors

mindaugasjasiunas avatar

Watchers

 avatar  avatar

restapi's Issues

Spring Profiles

Create Spring profiles for development and production using different DB's

Make REST stateless

As per the REST architecture, a RESTful Web Service should not keep a client state on the server. This restriction is called Statelessness.

In REST applications, each request must contain all of the information necessary to be understood by the server, rather than be dependent on the server remembering prior requests.

Storing session state on the server violates the stateless constraint of the REST architecture. So the session state must be handled entirely by the client.

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.