Sample REST API for managing tasks using Spring Boot and Jersey
Example of REST API using:
- Spring Boot: Framework for creating standalone Java applications.
- Jersey: JAX-RS reference implementation for creating RESTful web services in Java.
- Jackson: JSON parser for Java.
- MapStruct: Mapping framework for Java.
- Hibernate Validator: Bean Validation implemetation to define and validate application constraints.
- REST Assured: Testing framework for REST APIs.
Besides the REST API, it features a client application built with Angular and TypeScript. See the tasks-client-angular
project for details.
Building and running this application
To build and run this application, follow these steps:
- Open a command line window or terminal.
- Navigate to the root directory of the project, where the
pom.xml
resides. - Compile the project:
mvn clean compile
. - Package the application:
mvn package
. - Change into the
target
directory:cd target
- You should see a file with the following or a similar name:
tasks-1.0.jar
. - Execute the JAR:
java -jar tasks-1.0.jar
. - The REST API will be available at
http://localhost:8080/api
. - A JavaScript client application will be available at
http://localhost:8080
.
When the application starts up, the database will be populated with some rows.
Angular client application
An Angular and TypeScript client application is shipped with the main application and it’s available at http://localhost:8080
:
For better maintainability, client and server applications source code are kept in different repositories. During the build, the client application artifacts are downloaded, packed and released as part of the main application.
For the client application source code, refer to the tasks-client-angular
project.
REST API overview
The application provides a REST API for managing tasks. See the curl scripts below with the supported operations:
Create a task
curl -X POST \
'http://localhost:8080/api/tasks' \
-H 'Content-Type: application/json' \
-d '{
"description": "Pay internet bill"
}'
Get multiple tasks
curl -X GET \
'http://localhost:8080/api/tasks' \
-H 'Accept: application/json'
This endpoint supports the following query parameters:
description
(string): Filter tasks by description (case-insensitive).completed
(boolean): Filter tasks by completed status.
Filtering tasks by description:
curl -X GET -G \
'http://localhost:8080/api/tasks' \
-H 'Accept: application/json' \
-d 'description=avocado'
Filtering tasks by completed status:
curl -X GET -G \
'http://localhost:8080/api/tasks' \
-H 'Accept: application/json' \
-d 'completed=true'
Filtering tasks by description and by completed status:
curl -X GET -G \
'http://localhost:8080/api/tasks' \
-H 'Accept: application/json' \
-d 'description=karate' \
-d 'completed=true'
Get a task by id
curl -X GET \
'http://localhost:8080/api/tasks/5' \
-H 'Accept: application/json'
Update a task
curl -X PUT \
'http://localhost:8080/api/tasks/5' \
-H 'Content-Type: application/json' \
-d '{
"description": "Pay electricity bill",
"completed": false
}'
Update a task completed status
curl -X PUT \
'http://localhost:8080/api/tasks/5/completed' \
-H 'Content-Type: application/json' \
-d '{
"value": true
}'
Delete a task by id
curl -X DELETE \
'http://localhost:8080/api/tasks/5'
Delete multiple tasks
curl -X DELETE \
'http://localhost:8080/api/tasks'
This endpoint supports the following query parameter:
completed
(boolean): Delete tasks by completed status.
And it can be used as following:
curl -X DELETE -G \
'http://localhost:8080/api/tasks' \
-d 'completed=true'
Targeting the REST API with Postman
Alternatively to curl, you can use Postman to target the REST API. The Postman collection files are available in the src/main/postman
directory.