当前位置: 动力学知识库 > 问答 > 编程问答 >

java - JsonMappingException: Unexpected token (START_OBJECT)

问题描述:

I'm having problem with the user of Codehaus Jackson. I have an object with the next attributes and mapper declaration:

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:

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: [email protected]; line: 1, column: 1]

Can anyone help me?

网友答案:
Principal:{"adminPrincipal":false,"displayName":"sdfas","groupAware":false,"name":"sdfas"}

Is not valid JSON. It needs to look like this

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

(without the Principal: at the start)

分享给朋友:
您可能感兴趣的文章:
随机阅读: