You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+69-2Lines changed: 69 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,10 +3,13 @@
3
3
This library provides several useful utilities helping to implement REST controllers for React Admin-based frontend.
4
4
5
5
## Compatibility
6
-
The library works as a Spring Boot starter. Current version is compatible with Spring Boot **3.2.x**.
6
+
`react-admin-utils` works as a Spring Boot starter. Current version is compatible with Spring Boot **3.2.x**.
7
7
8
+
The library is a part of the [Amplicode](https://amplicode.io/) project, however it does not bring additional dependencies and can be used in any Spring Boot project.
8
9
9
-
## Usage
10
+
The implementation heavily depends on Jackson (version used by the Spring Web).
11
+
12
+
## Adding library to the project
10
13
Maven:
11
14
```
12
15
<dependency>
@@ -24,3 +27,67 @@ dependencies {
24
27
```
25
28
26
29
## ObjectPatcher
30
+
#### Patching
31
+
The `io.amplicode.rautils.patch.ObjectPatcher` bean helps to implement `PATCH` REST endpoints.
32
+
33
+
The bean performs patching the passed object under the following set of conditions:
34
+
* Real [PATCH](https://datatracker.ietf.org/doc/html/rfc5789) semantics is needed:
35
+
* patch contains a subset of object attributes to modify
36
+
* attributes not included into the patch are not modified
37
+
* attributes can be reset to null
38
+
* Data transfer object is an **immutable** class (POJO with all-args constructor, or a Java 17 record).
**Note** that if DTO used in the endpoint is **mutable** (e.g. has setters for all attributes), then an existing method from the Jackson library can be used instead of the `ObjectPatcher`:
`PATCH` REST endpoints, unlike endpoints for other HTTP methods, cannot put `@Valid` on the request body argument, because the request body does not contain the full set of attributes that constitute the object to be validated.
74
+
75
+
Therefore, it is required to validate patched object in the endpoint code. Unfortunately, Spring doesn't provide one-liner API to perform such validation. As a shortcut to perform the validation, `ObjectPatcher` provides several methods:
76
+
77
+
```java
78
+
@Autowired
79
+
privateObjectMapper objectMapper;
80
+
81
+
// just validate
82
+
// public void ObjectPatcher#validate(Object target);
83
+
objectPatcher.validate(patchedDto);
84
+
85
+
// patch and validate right away
86
+
// public <T> T ObjectPatcher#patchAndValidate(T target, JsonNode patchJson)
In case of validation failure, these methods throw non-checked exception `io.amplicode.rautils.patch.PatchValidationException` that contains validation errors and is automatically processed by the Spring `org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver` (resulting in `400 Bad Request` response code).
0 commit comments