Spring Boot, MySQL, Spring Security, JWT, JPA, Rest API

Build Restful CRUD API for a blog using Spring Boot, Mysql, JPA and Hibernate.

Steps to Setup

1. Clone the application

git clone https://github.com/coma123/Spring-Boot-Blog-REST-API.git

2. Create Mysql database

create database blogapi
  • run src/main/resources/blogapi.sql

3. Change mysql username and password as per your installation

  • open src/main/resources/application.properties
  • change spring.datasource.username and spring.datasource.password as per your mysql installation

4. Run the app using maven

mvn spring-boot:run

The app will start running at http://localhost:8080

Explore Rest APIs

The app defines following CRUD APIs.

Auth

MethodUrlDecriptionSample Valid Request Body
POST/api/auth/signupSign upJSON
POST/api/auth/signinLog inJSON

Users

MethodUrlDescriptionSample Valid Request Body
GET/api/users/meGet logged in user profile 
GET/api/users/{username}/profileGet user profile by username 
GET/api/users/{username}/postsGet posts created by user 
GET/api/users/{username}/albumsGet albums created by user 
GET/api/users/checkUsernameAvailabilityCheck if username is available to register 
GET/api/users/checkEmailAvailabilityCheck if email is available to register 
POST/api/usersAdd user (Only for admins)JSON
PUT/api/users/{username}Update user (If profile belongs to logged in user or logged in user is admin)JSON
DELETE/api/users/{username}Delete user (For logged in user or admin) 
PUT/api/users/{username}/giveAdminGive admin role to user (only for admins) 
PUT/api/users/{username}/TakeAdminTake admin role from user (only for admins) 
PUT/api/users/setOrUpdateInfoUpdate user profile (If profile belongs to logged in user or logged in user is admin)JSON

Posts

MethodUrlDescriptionSample Valid Request Body
GET/api/postsGet all posts 
GET/api/posts/{id}Get post by id 
POST/api/postsCreate new post (By logged in user)JSON
PUT/api/posts/{id}Update post (If post belongs to logged in user or logged in user is admin)JSON
DELETE/api/posts/{id}Delete post (If post belongs to logged in user or logged in user is admin) 

Comments

MethodUrlDescriptionSample Valid Request Body
GET/api/posts/{postId}/commentsGet all comments which belongs to post with id = postId 
GET/api/posts/{postId}/comments/{id}Get comment by id if it belongs to post with id = postId 
POST/api/posts/{postId}/commentsCreate new comment for post with id = postId (By logged in user)JSON
PUT/api/posts/{postId}/comments/{id}Update comment by id if it belongs to post with id = postId (If comment belongs to logged in user or logged in user is admin)JSON
DELETE/api/posts/{postId}/comments/{id}Delete comment by id if it belongs to post with id = postId (If comment belongs to logged in user or logged in user is admin) 

Albums

MethodUrlDescriptionSample Valid Request Body
GET/api/albumsGet all albums 
GET/api/albums/{id}Get album by id 
POST/api/albumsCreate new album (By logged in user)JSON
PUT/api/albums/{id}Update album (If album belongs to logged in user or logged in user is admin)JSON
DELETE/api/albums/{id}Delete album (If album belongs to logged in user or logged in user is admin) 
GET/api/albums/{id}/photosGet all photos which belongs to album with id = id 

Photos

MethodUrlDescriptionSample Valid Request Body
GET/api/photosGet all photos 
GET/api/photos/{id}Get photo by id 
POST/api/photosCreate new photo (By logged in user)JSON
PUT/api/photos/{id}Update photo (If photo belongs to logged in user or logged in user is admin)JSON
DELETE/api/photos/{id}Delete photo (If photo belongs to logged in user or logged in user is admin) 

Todos

MethodUrlDescriptionSample Valid Request Body
GET/api/todosGet all todos which belongs to logged in user 
GET/api/todos/{id}Get todo by id (If todo belongs to logged in user) 
POST/api/todosCreate new todo (By logged in user)JSON
PUT/api/todos/{id}Update todo (If todo belongs to logged in user)JSON
DELETE/api/todos/{id}Delete todo (If todo belongs to logged in user) 
PUT/api/todos/{id}/completeMark todo as complete (If todo belongs to logged in user) 
PUT/api/todos/{id}/unCompleteMark todo as uncomplete (If todo belongs to logged in user) 

Test them using postman or any other rest client.

Sample Valid JSON Request Bodys

Sign Up -> /api/auth/signup

{
    "firstName": "Leanne",
    "lastName": "Graham",
    "username": "leanne",
    "password": "password",
    "email": "leanne.graham@gmail.com"
}

Log In -> /api/auth/signin

{
    "usernameOrEmail": "leanne",
    "password": "password"
}

Create User -> /api/users

{
    "firstName": "Ervin",
    "lastName": "Howell",
    "username": "ervin",
    "password": "password",
    "email": "ervin.howell@gmail.com",
    "address": {
        "street": "Victor Plains",
        "suite": "Suite 879",
        "city": "Wisokyburgh",
        "zipcode": "90566-7771",
        "geo": {
            "lat": "-43.9509",
            "lng": "-34.4618"
        }
    },
    "phone": "010-692-6593 x09125",
    "website": "http://erwinhowell.com",
    "company": {
        "name": "Deckow-Crist",
        "catchPhrase": "Proactive didactic contingency",
        "bs": "synergize scalable supply-chains"
    }
}

Update User -> /api/users/{username}

{
    "firstName": "Ervin",
    "lastName": "Howell",
    "username": "ervin",
    "password": "updatedpassword",
    "email": "ervin.howell@gmail.com",
    "address": {
        "street": "Victor Plains",
        "suite": "Suite 879",
        "city": "Wisokyburgh",
        "zipcode": "90566-7771",
        "geo": {
            "lat": "-43.9509",
            "lng": "-34.4618"
        }
    },
    "phone": "010-692-6593 x09125",
    "website": "http://erwinhowell.com",
    "company": {
        "name": "Deckow-Crist",
        "catchPhrase": "Proactive didactic contingency",
        "bs": "synergize scalable supply-chains"
    }
}

Update User Profile -> /api/users/setOrUpdateInfo

{
    "street": "Douglas Extension",
    "suite": "Suite 847",
    "city": "McKenziehaven",
    "zipcode": "59590-4157",
    "companyName": "Romaguera-Jacobson",
    "catchPhrase": "Face to face bifurcated interface",
    "bs": "e-enable strategic applications",
    "website": "http://ramiro.info",
    "phone": "1-463-123-4447",
    "lat": "-68.6102",
    "lng": "-47.0653"
}

Create Post -> /api/posts

{
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto"
}

Update Post -> /api/posts/{id}

{
    "title": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED",
    "body": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED "
}

Create Comment -> /api/posts/{postId}/comments

{
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos tempora quo necessitatibus dolor quam autem quasi reiciendis et nam sapiente accusantium"
}

Update Comment -> /api/posts/{postId}/comments/{id}

{
    "body": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED "
}

Create Album -> /api/albums

{
    "title": "quidem molestiae enim"
}

Update Album -> /api/albums/{id}

{
    "title": "quidem molestiae enim UPDATED"
}

Create Photo -> /api/photos

{
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "https://via.placeholder.com/600/92c952",
    "thumbnailUrl": "https://via.placeholder.com/150/92c952",
    "albumId": 2
}

Update Photo -> /api/photos{id}

{
    "title": "accusamus beatae ad facilis ",
    "url": "https://via.placeholder.com/600/771796",
    "thumbnailUrl": "https://via.placeholder.com/150/771796",
    "albumId": 4
}

Create Todo -> /api/todos

{
    "title": "delectus aut autem",
    "completed": false
}

Update Todo -> /api/todos{id}

{
    "title": "delectus aut autem Updated",
    "completed": true
}

Download Details

Leave a Reply

Your email address will not be published. Required fields are marked *