java JsonMappingException: 意外令牌 (START_OBJECT)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31242819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JsonMappingException: Unexpected token (START_OBJECT)
提问by gleX
I'm having problem with the user of Codehaus Hymanson. I have an object with the next attributes and mapper declaration:
我遇到了 Codehaus Hymanson 用户的问题。我有一个具有下一个属性和映射器声明的对象:
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);
}
}
}
That is used from another class:
这是从另一个类使用的:
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);
}
}
}
When I execute the funtionality that generate a String like this:
当我执行生成这样的字符串的功能时:
Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}
When the method AuthenticatedPrincipal.deserialize(encodedPrincipal); is called parsing a Json parameter but that method fail with this error:
当方法 AuthenticatedPrincipal.deserialize(encodedPrincipal); 被称为解析 Json 参数,但该方法因以下错误而失败:
org.codehaus.Hymanson.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]
Can anyone help me?
谁能帮我?
回答by Simon
Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}
Is not valid JSON. It needs to look like this
不是有效的 JSON。它需要看起来像这样
{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}
(without the Principal:
at the start)
(没有Principal:
开头)