This simple project shows an implementation of a one-to-one relationship between two entity classes in Spring Boot.
- Request 1.
curl -X POST -H "Content-Type:application/json" -d '{"name":"My Library"}' http://localhost:8081/libraries
which returns
{"id":1,"name":"My Library"}
- Request 2.
curl -X POST -H "Content-Type:application/json" -d '{"location":"Main Street nr 5"}' http://localhost:8081/addresses
which returns
{"id":2,"location":"Main Street nr 5","library":null}
- Request 3.
curl -i -X PUT http://localhost:8081/libraries/1/address/2
which returns
{"id":1,"name":"My Library"}
- Request 4.
curl -X GET http://localhost:8081/addresses/2
which returns
{"id":2,"location":"Main Street nr 5","library":{"id":1,"name":"My Library"}}
Note that the value of "library"
has been updated from null
to {"id":1,"name":"My Library"}
.
In Windows, you need to escape additional single or double quotes with a backslash; for example:
curl -X POST -H "Content-Type:application/json" -d "{\"name\":\"My Library\"}" http://localhost:8081/libraries