Simple CRUD API for Animal Objects
0.0.1
- Get the project
- clone
git clone https://github.com/DeadeyeFo/CRUD-API.git - OR download zip.
- clone
- Open the project in your IDE (I used VSCode).
- This project is built to run with jdk 21.
/src/main/resources/application.propertiesfile is the configuration for the MySQL database on your localhost.- the database name is on the
datasource.urlproperty between the last/and the?. In this case the database name isanimal-database. - You MUST have the database up and running before running the project!
- Start your AMPPS Server.
- Click on the Home icon to open the localhost server on your browser.
- Go to Database Tools and open phpMyAdmin to start up the MySQL Dashboard.
- Ensure the database that you need is available. Either
- Create a database called
animal-database - OR edit
datasource.urlto point to a database that you do have.
- Create a database called
- Verify your username and password is spelled correctly in the properties file.
- the database name is on the
- Build and run the main class. You should see a new table created in the aforementioned database.
- Entity
- The Animal class is annotated as an
@Entity. This is used by Hibernate (an implementation of the Jakarta Persistence API) to map class attributes to database tables and SQL types. - We also annotated with
@Tableto give Hibernate directions to use this specific table name. This is optional but it helps with naming conventions. - Any Entity must have at least one attribute that is annotated as an
@Id. In our case it's conveniently theanimalIdattribute.- We are also using an autogeneration strategy for the ID. This way we are not manually assigning IDs to our animals. This is optional.
- An Entity must have a no-argument constructor.
- The Animal class is annotated as an
- Repository
- We are using an extension of the JPA Repository that comes with prebuilt database operations such as select all, select by id, select by any other reference, insert, delete, etc.
- Annotate it as a
@Repository. - We parametrize this using our object and its ID type.
public interface animalRepository extends JpaRepository<animal, Integer>=> We want to apply the JPA repository operations on theanimaltype. Theanimalhas an ID of typeint.
- Service
- Annotated as a
@Service. - It is the go-between from controller to database. In here we define what functions we need from the repository. A lot of the functions are default functions that our repository inherits from JPA (save, delete, findAll, findByX), some of them are custom made (getHonorsanimals, getanimalsByName).
- It asks the repository to perform SQL queries.
- Annotated as a
- Rest Controller
- Annotated as a
@RestController. - All the API endpoints mapped inside this controller will start with
/animals. - Return a Response Object.
- It asks the Service class to perform data access functions.
- Annotated as a
Base URL: http://localhost:8080/animals
/all (GET)
Gets a list of all Animals in the database.
[
{
"animalId": 888,
"name": "Axolotl",
"description": "The axolotl is a unique amphibian native to Mexico, known for its ability to regenerate limbs. Unlike most salamanders, it remains aquatic throughout its life. With feathery external gills and a perpetual smile, this critter thrives in freshwater lakes and is critically endangered."
},
{
"animalId": 889,
"name": "Quokka",
"description": "The quokka is a small, herbivorous marsupial native to Australia, often called \"the world's happiest animal\" due to its friendly, smiling expression. It thrives on Rottnest Island, is nocturnal, and can survive long periods without water by storing fat in its tail.."
}
]
/{animalId} (GET)
Gets an individual animal in the system. Each animal is identified by a numeric animalId
- Path Variable:
animalId<integer> - REQUIRED
[
{
"animalId": 888,
"name": "Axolotl",
"description": "The axolotl is a unique amphibian native to Mexico, known for its ability to regenerate limbs. Unlike most salamanders, it remains aquatic throughout its life. With feathery external gills and a perpetual smile, this critter thrives in freshwater lakes and is critically endangered."
}
]
/name (GET)
Gets a specific animal by its name that contains the given string.
- query parameter:
searchlt;String> - REQUIRED
[
{
"animalId": 889,
"name": "Quokka",
"description": "The quokka is a small, herbivorous marsupial native to Australia, often called \"the world's happiest animal\" due to its friendly, smiling expression. It thrives on Rottnest Island, is nocturnal, and can survive long periods without water by storing fat in its tail.."
}
]
/new (POST)
Create a new animal entry
A animal object. Note that the animalId is auto assigned in the database so is not needed in the request.
{
"name": "Jaguar",
"description": "The jaguar is a powerful big cat native to the Americas, known for its rosette-patterned coat and immense bite force, capable of crushing bones and turtle shells. A stealthy apex predator, it thrives in rainforests and wetlands, often hunting by ambush and swimming expertly."
}
[
{
"animalId": 888,
"name": "Axolotl",
"description": "The axolotl is a unique amphibian native to Mexico, known for its ability to regenerate limbs. Unlike most salamanders, it remains aquatic throughout its life. With feathery external gills and a perpetual smile, this critter thrives in freshwater lakes and is critically endangered."
},
{
"animalId": 889,
"name": "Quokka",
"description": "The quokka is a small, herbivorous marsupial native to Australia, often called \"the world's happiest animal\" due to its friendly, smiling expression. It thrives on Rottnest Island, is nocturnal, and can survive long periods without water by storing fat in its tail.."
},
{
"animalId": 890,
"name": "Jaguar",
"description": "The jaguar is a powerful big cat native to the Americas, known for its rosette-patterned coat and immense bite force, capable of crushing bones and turtle shells. A stealthy apex predator, it thrives in rainforests and wetlands, often hunting by ambush and swimming expertly."
}
]
/update/{animalId} (PUT)
Update an existing animal.
- Path Variable:
animalId<integer> - REQUIRED
A animal object with the updates.
{
"animalId": 890,
"name": "Jaguar",
"description": "jaguar is a powerful big cat native to the Americas, known for its rosette-patterned coat and immense bite force, capable of crushing bones and turtle shells. A stealthy apex predator, it thrives in rainforests and wetlands, often hunting by ambush and swimming expertly."
}
{
"animalId": 890,
"name": "Jaguar",
"description": "jaguar is a powerful big cat native to the Americas, known for its rosette-patterned coat and immense bite force, capable of crushing bones and turtle shells. A stealthy apex predator, it thrives in rainforests and wetlands, often hunting by ambush and swimming expertly."
}
/delete/{animalId} (DELETE)
Delete an existing animal.
- Path Variable:
animalId<integer> - REQUIRED
[
{
"animalId": 888,
"name": "Axolotl",
"description": "The axolotl is a unique amphibian native to Mexico, known for its ability to regenerate limbs. Unlike most salamanders, it remains aquatic throughout its life. With feathery external gills and a perpetual smile, this critter thrives in freshwater lakes and is critically endangered."
},
{
"animalId": 889,
"name": "Quokka",
"description": "The quokka is a small, herbivorous marsupial native to Australia, often called \"the world's happiest animal\" due to its friendly, smiling expression. It thrives on Rottnest Island, is nocturnal, and can survive long periods without water by storing fat in its tail.."
}
]