I am not sure what you mean by « implementing JSON Patch to an Object (JSON model) » but you may be interested in having a look at Java implementations of JSON-Patch such as zjsonpatch or fge's json-patch.

I hope this helps.

Answer from ctranxuan on Stack Overflow
🌐
GitHub
github.com › java-json-tools › json-patch
GitHub - java-json-tools/json-patch: An RFC 6902 (JSON Patch) and reverse, plus RFC 7386 (JSON Merge Patch), implementation in Java using Jackson (2.2.x) · GitHub
This is an implementation of RFC 6902 (JSON Patch) and RFC 7386 (JSON Merge Patch) written in Java, which uses Jackson (2.2.x) at its core.
Starred by 693 users
Forked by 196 users
Languages   Java 99.9% | Shell 0.1%
🌐
Baeldung
baeldung.com › home › spring › spring web › spring mvc › using json patch in spring rest apis
Using JSON Patch in Spring REST APIs | Baeldung
May 11, 2024 - When a patch request with the application/json-patch+json “Content-Type” arrives, Spring Boot uses the default MappingJackson2HttpMessageConverter to convert the request payload to a JsonPatch instance. As a result, our controller method will receive the request body as a JsonPatch instance ... Subsequently, if the customer record is found, we invoke the applyPatchToCustomer(patch, customer) method.
🌐
JAXB
javaee.github.io › javaee-spec › javadocs › javax › json › JsonPatch.html
JsonPatch (Java(TM) EE 8 Specification APIs)
JsonPatchBuilder builder = Json.createPatchBuilder(); JsonArray result = builder.add("/John/phones/office", "1234-567") .remove("/Amy/age") .build() .apply(contacts); ... Applies the patch operations to the specified target.
🌐
Jsonpatch
jsonpatch.com
JSON Patch | jsonpatch.com
JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed.
🌐
Medium
bijukunjummen.medium.com › json-patch-and-json-merge-patch-quick-example-in-java-c36fdabc2810
Json Patch and Json Merge Patch — Quick Example in Java | by Biju Kunjummen | Medium
May 8, 2021 - Json Patch does it as a series of operations which transforms a source document and Json Merge Patch represents the change as a lite version of the source document.
🌐
Javadoc.io
javadoc.io › doc › com.box › json-patch › latest › com › github › fge › jsonpatch › JsonPatch.html
JsonPatch - json-patch 1.16 javadoc
Latest version of com.box:json-patch · https://javadoc.io/doc/com.box/json-patch · Current version 1.16 · https://javadoc.io/doc/com.box/json-patch/1.16 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/com.box/json-patch/1.16/package-list ·
🌐
GitHub
github.com › tananaev › json-patch
GitHub - tananaev/json-patch: A JSON patch (RFC 6902) library · GitHub
Java implementation of RFC 6902 (JSON Patch) standard.
Starred by 18 users
Forked by 12 users
Languages   Java
Find elsewhere
🌐
GitHub
github.com › flipkart-incubator › zjsonpatch
GitHub - flipkart-incubator/zjsonpatch: This is an implementation of RFC 6902 JSON Patch written in Java · GitHub
This is an implementation of RFC 6902 JSON Patch written in Java - flipkart-incubator/zjsonpatch
Starred by 573 users
Forked by 156 users
Languages   Java
🌐
Maven Repository
mvnrepository.com › artifact › com.github.java-json-tools › json-patch
Maven Repository: com.github.java-json-tools » json-patch
Home » com.github.java-json-tools » json-patch · JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386) implementation in Java · Central (4) Redhat GA (5) Mulesoft (4) Central · Atlassian External · Atlassian · WSO2 Releases · WSO2 Public · Hortonworks ·
🌐
Stack Overflow
stackoverflow.com › questions › 66891641 › get-modify-specific-data-from-jsonpatch-request
java - Get/Modify specific data from jsonPatch request - Stack Overflow
Now in the below code, I want a loop for this JSONPatch object to modify some values (for example name). public void patch(JsonPatch jsonPatch) throws JsonPatchException { // need a foreach here to access JSONPatch object to modify some values jsonPatch.apply(objectMapper.convertValue(myObject, JsonNode.class)); }
🌐
Java && More
gaetanopiazzolla.github.io › java › jsonpatch › springboot › 2024 › 09 › 25 › boot-patch.html
Spring Boot JSON Patch done Right | Java && More
September 25, 2024 - JSON Patch is applied to JPA entities in a Spring Boot Application. This approach is designed to be generic, reusable, and optimized, so it should be easy to apply to any Spring-Boot-based and JPA-based based project.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Json Patch and Json Merge Patch in Java - Java Code Geeks
January 3, 2022 - Json Patch does it as a series of operations which transforms a source document and Json Merge Patch represents the change as a lite version of the source document.
🌐
Hacker News
news.ycombinator.com › item
What Is JSON Patch? | Hacker News
May 18, 2022 - JSON Patch is not REST. It is not representational state. But it's also not the way any sane person has ever done RPC over HTTP. Typically RPCs accomplish mutation by just supplying whatever is the most ergonomic at the time; if you want to be able to rename an entity, you implement a /rename ...
🌐
Maven Central
central.sonatype.com › artifact › com.github.java-json-tools › json-patch
com.github.java-json-tools:json-patch - Maven Central
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.java-json-tools</groupId> <artifactId>json-patch</artifactId> <version>1.13</version> <name>json-patch</name> <description>JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386) implementation in Java</description> <url>https://github.com/java-json-tools/json-patch</url> <licenses> <license> <name>Les
🌐
Medium
medium.com › @victorjonah › heard-of-json-patching-74ea85647570
Heard of JSON Patching?. Have you heard about JSON Patch? | by Victor Jonah | Medium
May 26, 2020 - PUT updates the whole resource while PATCH updates only the specified change. It expresses/recounts/depicts a JSON document. It applies operations to a JSON document, this operations with its values to be manipulated are also in a JSON document contained in an array.
🌐
Zuplo
zuplo.com › home › learning center › unlocking the power of json patch
Unlocking the Power of JSON Patch - Zuplo
October 10, 2024 - fast-json-patch: A Javascript node JS package for generating and applying JSON patches.
Top answer
1 of 4
10

Under many circumstances you can just patch an intermediate object which only has fields that the user can write to. After that you could quite easily map the intermediate object to your entity, using some object mapper or just manually.

The downside of this is that if you have a requirement that fields must be explicitly nullable, you won’t know if the patch object set a field to null explicitly or if it was never present in the patch.

What you can do too is abuse Optionals for this, e.g.

public class ProjectPatchDTO {

    private Optional<@NotBlank String> name;
    private Optional<String> description;
}

Although Optionals were not intended to be used like this, it's the most straightforward way to implement patch operations while maintaining a typed input. When the optional field is null, it was never passed from the client. When the optional is not present, that means the client has set the value to null.

2 of 4
1

Instead of receiving a JsonPatch directly from the client, define a DTO to handle the validation and then you will later convert the DTO instance to a JsonPatch.

Say you want to update a user of instance User.class, you can define a DTO such as:

public class UserDTO {

    @Email(message = "The provided email is invalid")
    private String username;

    @Size(min = 2, max = 10, message = "firstname should have at least 2 and a maximum of 10 characters")
    private String firstName;

    @Size(min = 2, max = 10, message = "firstname should have at least 2 and a maximum of 10 characters")
    private String lastName;

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

//getters and setters
}

The custom toString method ensures that fields that are not included in the update request are not prefilled with null values.

Your PATCH request can be as follows(For simplicity, I didn't cater for Exceptions)

@PatchMapping("/{id}")
    ResponseEntity<Object> updateUser(@RequestBody @Valid UserDTO request,
                                      @PathVariable String id) throws ParseException, IOException, JsonPatchException {
        User oldUser = userRepository.findById(id);
        String detailsToUpdate = request.toString();
        User newUser = applyPatchToUser(detailsToUpdate, oldUser);
        userRepository.save(newUser);
        return userService.updateUser(request, id);
    }

The following method returns the patched User which is updated above in the controller.

private User applyPatchToUser(String detailsToUpdate, User oldUser) throws IOException, JsonPatchException {
        ObjectMapper objectMapper = new ObjectMapper();
        // Parse the patch to JsonNode
        JsonNode patchNode = objectMapper.readTree(detailsToUpdate);
        // Create the patch
        JsonMergePatch patch = JsonMergePatch.fromJson(patchNode);
        // Convert the original object to JsonNode
        JsonNode originalObjNode = objectMapper.valueToTree(oldUser);
        // Apply the patch
        TreeNode patchedObjNode = patch.apply(originalObjNode);
        // Convert the patched node to an updated obj
        return objectMapper.treeToValue(patchedObjNode, User.class);
    }