我目前正在为azuresdkforjava修复一个bug,当我试图将一些json反序列化到一个带有可选的类中时,我得到了一个invliddefinitionexception。我做错什么了?我的pom.xml中有以下依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.11.2</version>
</dependency>
下面是执行反序列化的代码:
final ObjectMapper objectMapper = JacksonObjectMapperFactory.getInstance();
objectMapper.registerModule(new Jdk8Module());
UserGroups groupsFromJson = objectMapper.readValue(responseInJson, UserGroups.class);
public final class JacksonObjectMapperFactory {
private JacksonObjectMapperFactory() {
}
public static ObjectMapper getInstance() {
return SingletonHelper.INSTANCE;
}
private static class SingletonHelper {
private static final ObjectMapper INSTANCE = new ObjectMapper();
}
}
下面是用于反序列化的Map类:
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserGroups {
@JsonProperty("odata.nextLink")
private Optional<String> odataNextLink;
@JsonProperty("value")
private List<UserGroup> value;
public UserGroups(Optional<String> odataNextLink, List<UserGroup> value) {
this.odataNextLink = odataNextLink;
this.value = value;
}
public Optional<String> getOdataNextLink() {
return odataNextLink;
}
public List<UserGroup> getValue() {
return value;
}
}
下面是usergroup类:
import java.io.Serializable;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserGroup implements Serializable {
private static final long serialVersionUID = 9064197572478554735L;
@JsonProperty("objectId")
@JsonAlias("id")
private String objectID;
@JsonProperty("objectType")
@JsonAlias("@odata.type")
private String objectType;
@JsonProperty("displayName")
private String displayName;
public UserGroup(String objectID, String objectType, String displayName) {
this.objectID = objectID;
this.objectType = objectType;
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
public String getObjectType() {
return objectType;
}
public String getObjectID() {
return objectID;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof UserGroup)) {
return false;
}
final UserGroup group = (UserGroup) o;
return this.getDisplayName().equals(group.getDisplayName())
&& this.getObjectID().equals(group.getObjectID())
&& this.getObjectType().equals(group.getObjectType());
}
@Override
public int hashCode() {
return Objects.hash(objectID, objectType, displayName);
}
}
以下是运行测试时出现的错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `com.microsoft.azure.spring.autoconfigure.aad.UserGroups` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"odata.metadata":"https://graph.windows.net/myorganization/$metadata#directoryObjects","value":[{"odata.type":"Microsoft.DirectoryServices.Group","objectType":"Group","objectId":"12345678-7baf-48ce-96f4-a2d60c26391e","deletionTimestamp":null,"description":"this is group1","dirSyncEnabled":true,"displayName":"group1","lastDirSyncTime":"2017-08-02T12:54:37Z","mail":null,"mailNickname":"something","mailEnabled":false,"onPremisesDomainName":null,"onPremisesNetBiosName":null,"onPremisesSamAccountNam"[truncated 1554 chars]; line: 1, column: 2]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1615)
这是我想反序列化的json示例:
{
"odata.metadata":"https://graph.windows.net/myorganization/$metadata#directoryObjects",
"odata.nextLink":"directoryObjects/$/Microsoft.DirectoryServices.User/userId/memberOf?$skiptoken=X'XXXXXXXX'",
"value":[
{
"odata.type":"Microsoft.DirectoryServices.Group",
"objectType":"Group",
"objectId":"12345678-7baf-48ce-96f4-a2d60c26391e",
"deletionTimestamp":null,
"description":"this is group1",
"dirSyncEnabled":true,
...
}
]
}
我不是很有经验的Jackson,任何帮助是非常感谢。谢谢你读了这么远!
1条答案
按热度按时间uz75evzq1#
改变你的想法
UserGroup.java
我们的课程是这样的:以及
UserGroups.java
课程还包括:在那之后,一切都会好起来的。