Java ObjectIdResolver-class And Method Code Example

I apologize, but the ObjectIdResolver is not a class from the Apache Commons IO library. It is a part of the Jackson JSON library, which is a separate library for handling JSON data.

ObjectIdResolver is used for resolving object ids during deserialization. It allows for resolving object ids to actual objects and keeping track of objects that have already been deserialized.

Here is an example of how to use the ObjectIdResolver with Jackson JSON library:

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId;

import java.util.HashMap;
import java.util.Map;

public class Example {

    @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
    public static class User {
        private String name;
        private int age;
        // getters and setters

    public static class UserIdResolver extends ObjectIdResolver {

        private Map<Integer, User> users = new HashMap<>();

        public void bindItem(ReadableObjectId objectId, Object pojo) {
            User user = (User) pojo;
            users.put(objectId.getKey().asInt(), user);

        public Object resolveId(ReadableObjectId objectId) {
            return users.get(objectId.getKey().asInt());

        public ObjectIdResolver newForDeserialization(Object context) {
            return new UserIdResolver();

        public boolean canUseFor(ObjectIdResolver resolverType) {
            return false;

    public static void main(String[] args) throws Exception {
        String json = "[{\"@id\":1,\"name\":\"John\",\"age\":25},{\"@id\":2,\"name\":\"Jane\",\"age\":30},{\"@id\":1,\"name\":\"John\",\"age\":25}]";

        ObjectMapper mapper = new ObjectMapper();
        mapper.setObjectIdResolver(new UserIdResolver());
        User[] users = mapper.readValue(json, User[].class);

        System.out.println("Deserialized users:");
        for (User user : users) {
            System.out.println(user.getName() + " - " + user.getAge());

In this example, User class is annotated with JsonIdentityInfo which tells Jackson to use ObjectIdGenerators.IntSequenceGenerator for generating object ids and to use the @id property for storing the id in the JSON.

The UserIdResolver class is a custom ObjectIdResolver that keeps track of the deserialized User objects in a `Map