0
votes

We work on Spring Boot and oracle LDAP (Oid) with Spring LDAP module. The connection to LDAP with spring is ok but when we want to create LDAP group with ldapTemplate.create() method we got this error from Jackson: please help us that what's wrong here!thanks

Type definition error: [simple type, class javax.naming.Name]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of javax.naming.Name (no Creators, like default construct, exist)

//Group Entry
    @Getter
    @Setter
    @Entry(objectClasses = {"top", "groupOfUniqueNames"}, base = "cn=Groups")
    public final class Group {

        private static final String BASE_DN = "dc=eis,dc=msc,dc=ir";

        @Id
        private Name dn;

        @Attribute(name="cn")
        @DnAttribute("cn")
        private String name;

        @Attribute(name = "displayName")
        private String description;

        @Attribute(name = "owner")
        private String owner;

        @Attribute(name="uniqueMember")
        private Set members;

        public Group() {
        }

        public Group(String name, Set members) {
                Name dn =  LdapNameBuilder.newInstance(BASE_DN)
                        .add("ou", "groups")
                        .add("cn", name)
                        .build();
                this.dn = dn;
                this.name = name;
                this.members = members;
        }

        public Group(Name dn, String name, String description, String owner, Set members) {
            this.dn = dn;
            this.name = name;
            this.description = description;
            this.owner = owner;
            this.members = members;
        }

        public void addMember(Name member) {
            if (this.members == null){
                this.members = new HashSet<>();
            }
            members.add(member);
        }

        public void removeMember(Name member) {
            members.remove(member);
        }

        @Override
        public String toString() {
            return "Group{" +
                    "dn=" + dn +
                    ", name='" + name + '\'' +
                    ", members=" + members +
                    '}';
        }

    //Service

    @Override
        public int createOIDGroupByJob(Group group) {
            try {
                ldapTemplate.create(group);
                return 1;
            }catch (Exception e){
                return 0;
            }
        }

    @PostMapping("/api/ldap/group")
            public ResponseEntity<?> add(@RequestBody Group group) {
                int retVal = ldapService.createOIDGroupByJob(group);
                if (retVal==1) {
                    return new ResponseEntity<>(HttpStatus.OK);
                } else {
                    return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
                }
            }
//in postman

    {
    "dn": {
        "rdns": [
            {
                "value": "Groups",
                "type": "cn"
            },
            {
                "value": "some data",
                "type": "cn"
            }
        ]
    },
    "fullName": "some data",
    "lastName": "some data"

}
1
i think the problem is for jackson Deserializtion - soheil qalamkari

1 Answers

0
votes

Finally!! I solved the problem!the problem is the jackson want to Deserialized an interface!! (javax.naming.Name) and it is not woking!

I found the solution is that use @JsonCreator on constructor like this:

//User Ldap Entry
 public User(Name dn, String name, String lastName, String group) {
        this.dn = dn;
        this.name = name;
        this.lastName = lastName;
        this.group = group;
    }

    @JsonCreator
    public User(@JsonProperty("dn") @JsonDeserialize(as=LdapName.class) final Name dn) {
        this.dn = dn;
    }

By the above sample, in @JsonDeserialize(as=LdapName.class) I pass LdapName.class that is the one of Name implemention! it works fine:)