A small example in the form of a note-taking application built using Spring Boot for the REST API with H2 for a database. Demonstrates elementary CRUD operations in response to HTTP as well as returning JSON.
Make sure to edit your application.properties
and change the database information to reflect where you would like H2 to create the DB file.
It is currently set to the directly from which you are compiling the application.
spring.datasource.url=jdbc:h2:file:./notesDB
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=berti
spring.datasource.password=
To test out this application, you need Maven to build the dependencies.
- First, install the dependencies
mvn clean install
- Second, run the production build with live reload
mvn spring-boot:run
When the application is first built, it will create a database file in the directory specified in the application.properties
file.
mvn test
The notes API lives at the route /api/notes
. If your application is running on localhost:8080, you would access the API via http://localhost:8080/api/notes.
{
"id" : 1,
"body" : "Ask Larry about the TPS reports."
}
To create a new note, post a JSON payload to the API endpoint as modeled below:
POST /api/notes
BODY a note
Returns: a saved note... Example
curl -i -H "Content-Type: application/json" -X POST -d '{"body" : "Pick up milk!"}' http://localhost:8080/api/notes
Returns:
{
"id" : 2,
"body" : "Pick up milk!"
}
Get a note using an API call:
GET /api/notes/{id}
Returns: the requested note.. Example:
curl -i -H "Content-Type: application/json" -X GET http://localhost:8080/api/notes/1
Returns:
{
"id" : 1,
"body" : "Ask Larry about the TPS reports."
}
I can get all notes using an API call:
GET /api/notes
Returns: A list of my notes
Example:
curl -i -H "Content-Type: application/json" -X GET http://localhost:8080/api/notes
Returns:
[
{
"id" : 2,
"body" : "Pick up milk!"
},
{
"id" : 1,
"body" : "Ask Larry about the TPS reports."
}
]
To search notes by their bodies, use the 'query' parameter in the GET request Example:
curl -i -H "Content-Type: application/json" -X GET http://localhost:8080/api/notes?query=milk
Returns a list of every note with the word 'milk' in it.
To delete an individual note use the endpoint /api/notes/delete
with a parameter of 'id' signifying the note you wish to remove.
POST /api/notes/delete
ID int value of id to delete
Returns: the updated list of notes... Example
curl -i -H "Content-Type: application/json" -X POST http://localhost:8080/api/notes/delete?id=1
- Maven - Dependency Management
- Spring Boot - Quick start Spring Framework web applications
- H2 Database Engine - the Java SQL database
- JSON.simple -JSON.simple is a simple Java toolkit for JSON
This project is licensed under the MIT License - see the LICENSE.md file for details