无法在杰克逊中构造“javax.naming.Name”的实例
Cannot construct instance of `javax.naming.Name` in jackson
我们使用 Spring LDAP 模块开发 Spring 引导和 oracle LDAP (Oid)。使用 spring 连接到 LDAP 是可以的,但是当我们想使用 ldapTemplate.create()
方法创建 LDAP 组时,我们从 Jackson 那里得到了这个错误:
请帮我们看看这里出了什么问题!谢谢
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"
}
终于!!我解决了问题!问题是杰克逊想要反序列化一个接口! (javax.naming.Name) 它没有醒来!
我发现解决方案是在构造函数上使用 @JsonCreator,如下所示:
//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;
}
通过上面的示例,在 @JsonDeserialize(as=LdapName.class) 中,我传递了 LdapName.class ,这是 Name 实现之一!它工作正常:)
我们使用 Spring LDAP 模块开发 Spring 引导和 oracle LDAP (Oid)。使用 spring 连接到 LDAP 是可以的,但是当我们想使用 ldapTemplate.create()
方法创建 LDAP 组时,我们从 Jackson 那里得到了这个错误:
请帮我们看看这里出了什么问题!谢谢
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"
}
终于!!我解决了问题!问题是杰克逊想要反序列化一个接口! (javax.naming.Name) 它没有醒来!
我发现解决方案是在构造函数上使用 @JsonCreator,如下所示:
//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;
}
通过上面的示例,在 @JsonDeserialize(as=LdapName.class) 中,我传递了 LdapName.class ,这是 Name 实现之一!它工作正常:)