Home

Awesome

Build Status Sonarcloud Status CII Best Practices

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

3. Change mysql username and password as per your 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

<a id="signup">Sign Up -> /api/auth/signup</a>
{
	"firstName": "Leanne",
	"lastName": "Graham",
	"username": "leanne",
	"password": "password",
	"email": "leanne.graham@gmail.com"
}
<a id="signin">Log In -> /api/auth/signin</a>
{
	"usernameOrEmail": "leanne",
	"password": "password"
}
<a id="usercreate">Create User -> /api/users</a>
{
	"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"
	}
}
<a id="userupdate">Update User -> /api/users/{username}</a>
{
	"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"
	}
}
<a id="userinfoupdate">Update User Profile -> /api/users/setOrUpdateInfo</a>
{
	"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"
}
<a id="postcreate">Create Post -> /api/posts</a>
{
	"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"
}
<a id="postupdate">Update Post -> /api/posts/{id}</a>
{
	"title": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED",
	"body": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED "
}
<a id="commentcreate">Create Comment -> /api/posts/{postId}/comments</a>
{
	"body": "laudantium enim quasi est quidem magnam voluptate ipsam eos tempora quo necessitatibus dolor quam autem quasi reiciendis et nam sapiente accusantium"
}
<a id="commentupdate">Update Comment -> /api/posts/{postId}/comments/{id}</a>
{
	"body": "UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED UPDATED "
}
<a id="albumcreate">Create Album -> /api/albums</a>
{
	"title": "quidem molestiae enim"
}
<a id="albumupdate">Update Album -> /api/albums/{id}</a>
{
	"title": "quidem molestiae enim UPDATED"
}
<a id="photocreate">Create Photo -> /api/photos</a>
{
	"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
}
<a id="photoupdate">Update Photo -> /api/photos{id}</a>
{
	"title": "accusamus beatae ad facilis ",
	"url": "https://via.placeholder.com/600/771796",
	"thumbnailUrl": "https://via.placeholder.com/150/771796",
	"albumId": 4
}
<a id="todocreate">Create Todo -> /api/todos</a>
{
	"title": "delectus aut autem",
	"completed": false
}
<a id="todoupdate">Update Todo -> /api/todos{id}</a>
{
	"title": "delectus aut autem Updated",
	"completed": true
}

segment