This is a Spring Boot application for Web based REST
Featuriz guide to Spring Framework 5.
Spring | Version |
---|---|
Spring Boot | 2.6.1 |
Spring web | 5.3.13 |
Spring Test | 5.3.13 |
Spring JPA | 2.6.1 |
Mariadb | 2.7.4 |
Lombok | 1.18.22 |
Validation | 2.6.1 |
This is a maven based spring boot project. Use maven to build this project and just run.
No other dependencies
Check application.properties file.
- Entities can create tables
- You can add mock content to db by using data.sql file - This can be load automatically, check properties file.
This work is based on the tutorial from
Building REST services with Spring
The difference between REST and RPC:
- REST - Representational State Transfer
- RPC - Remote Procedure Call
Working with Spring HATEOAS
-
This helps to write hypermedia-driven outputs.
org.springframework.boot spring-boot-starter-hateoas -
A critical ingredient to any RESTful service is adding links to relevant operations
{ "id": 1, "name": "Bilbo Baggins", "role": "burglar", "_links": { "self": { "href": "http://localhost:8080/employees/1" }, "employees": { "href": "http://localhost:8080/employees" } } }
- Create EmployeeModelAssembler by implementing RepresentationModelAssembler interface
- Convert Employee objects to EntityModel objects
Never delete a column in a database.
- Replace the field
- A "virtual" getter & setter for the old name property
-
Include a Location response header
-
Return the model-based version of the saved object
$ curl -v -X PUT localhost:8080/employees/3 -H 'Content-Type:application/json' -d '{"name": "Samwise Gamgee", "role": "ring bearer"}'
- TCP_NODELAY set
- Connected to localhost (::1) port 8080 (#0)
PUT /employees/3 HTTP/1.1 Host: localhost:8080 User-Agent: curl/7.54.0 Accept: / Content-Type:application/json Content-Length: 49
< HTTP/1.1 201 < Location: http://localhost:8080/employees/3 < Content-Type: application/hal+json;charset=UTF-8 < Transfer-Encoding: chunked < Date: Fri, 10 Aug 2018 19:52:56 GMT { "id": 3, "firstName": "Samwise", "lastName": "Gamgee", "role": "ring bearer", "name": "Samwise Gamgee", "_links": { "self": { "href": "http://localhost:8080/employees/3" }, "employees": { "href": "http://localhost:8080/employees" } } }
- Enter HATEOAS or Hypermedia as the Engine of Application State.
- Instead of clients parsing the payload, give them links to signal valid actions.
- Decouple state-based actions from the payload of data.
- In other words, when CANCEL and COMPLETE are valid actions, dynamically add them to the list of links.
- Clients only need show users the corresponding buttons when the links exist.
$ curl -v -X DELETE http://localhost:8080/orders/4/cancel
> DELETE /orders/4/cancel HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Mon, 27 Aug 2018 15:02:10 GMT
<
{
"id": 4,
"description": "iPhone",
"status": "CANCELLED",
"_links": {
"self": {
"href": "http://localhost:8080/orders/4"
},
"orders": {
"href": "http://localhost:8080/orders"
}
}
}
HTTP 200 status code indicating it was successful
If you try the same operation again…
$ curl -v -X DELETE http://localhost:8080/orders/4/cancel
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> DELETE /orders/4/cancel HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 405
< Content-Type: application/problem+json
< Transfer-Encoding: chunked
< Date: Mon, 27 Aug 2018 15:03:24 GMT
<
{
"title": "Method not allowed",
"detail": "You can't cancel an order that is in the CANCELLED status"
}
HTTP 405 Method Not Allowed