This is the code sample to accompany the blog post: Creating Java JPA entities with a JSON field.
This repository illustrates the usage of an JSON field within a JPA entity. The point is to show how you can use JPA entities and still have JSON persisted in your underlying PostgreSQL database.
Steps to see everything in action:
- Ensure that you have created the PostgreSQL DB, and the schema (see
src/main/resources/db
for the schema scripts). - Update the
src/main/resources/application.properties
file to have the correct values for your data source. - Run the
bootRun
Gradle task to launch the Spring Boot application. - Make a POST request to create a new Person
# Create a new person curl -X POST -H "Content-Type:application/json" -d '{ "firstName": "John", "surname": "Smith", "address": { "doorNumber": 2, "roadName": "London Road", "postalCode": "EC34AWE" } }' http://localhost:8080/people
- Check the DB - you will have stored JSON!
- Make a PUT request to update the Person
# Update an existing person curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "John", "surname": "Smith", "address": { "doorNumber": 35, "roadName": "New York Road", "postalCode": "12345" } }' http://localhost:8080/people/1
- Check the DB - your JSON would have been updated!
Enjoy!
Also - if you have not yet already: check out the LigerLearn website and YouTube channel.