gpt4 book ai didi

java - JsonMappingException : Unexpected token (START_OBJECT)

转载 作者:行者123 更新时间:2023-11-30 11:01:18 24 4
gpt4 key购买 nike

我对 Codehaus Jackson 的用户有疑问。我有一个具有下一个属性和映射器声明的对象:

public class AuthenticatedPrincipal implements Serializable, Principal {
@JsonIgnore
private final static ObjectMapper mapper = new ObjectMapper().enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY).enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)
.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL).setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);

private String name;

private Collection<String> roles;

private Collection<String> groups;

private boolean adminPrincipal;
...
@JsonIgnore
public String serialize() {
try {
return mapper.writeValueAsString(this);
} catch (IOException e) {
throw new RuntimeException("Unable to serialize Principal:" + toString(), e);
}
}

@JsonIgnore
public static AuthenticatedPrincipal deserialize(String json) {
try {
return mapper.readValue(json, AuthenticatedPrincipal.class);
} catch (IOException e) {
throw new RuntimeException("Unable to serialize Principal:" + json, e);
}
}
}

这是从另一个类中使用的:

public class AuthRequest {

@Transient
private AuthenticatedPrincipal principal;
@PreUpdate
@PrePersist
public void encodePrincipal() {
if (principal != null) {
this.encodedPrincipal = principal.serialize();
}
}

@PostLoad
@PostPersist
@PostUpdate
public void decodePrincipal() {
if (StringUtils.isNotBlank(encodedPrincipal)) {
this.principal = AuthenticatedPrincipal.deserialize(encodedPrincipal);
}
}
}

当我执行生成这样一个字符串的功能时:

Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

当方法AuthenticatedPrincipal.deserialize(encodedPrincipal);被称为解析 Json 参数,但该方法失败并出现此错误:

org.codehaus.jackson.map.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class com.trent.app.lib.principal.AuthenticatedPrincipal
at [Source: java.io.StringReader@40fa255; line: 1, column: 1]

谁能帮帮我?

最佳答案

Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

不是有效的 JSON。它需要看起来像这样

{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

(没有 Principal: 开头)

关于java - JsonMappingException : Unexpected token (START_OBJECT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com