0
我有從彈簧不可變對象SimpleGrantedAuthority
不具有一個無參數構造。相反,它是一個只有1單參數的構造類似這樣的不可變對象:配置傑克遜使用單個參數構造
public SimpleGrantedAuthority(String role) {
Assert.hasText(role, "A granted authority textual representation is required");
this.role = role;
}
我試圖反序列化一個字符串到該對象和我沒有訪問此文件的源代碼。這裏是例外,我得到:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.security.core.authority.SimpleGrantedAuthority: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: [email protected]; line: 7, column: 7] (through reference chain: com.nemesis.platform.facade.email.data.EnrichedEmailMessageContextData["customer"]->com.nemesis.platform.facade.user.data.EnrichedUserDetails["authorities"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
就像我說的,我沒有源代碼,我不能添加@JsonCreator
註解,所以我不知道如何配置傑克遜瞭解正確的構造器使用。東西的喜歡:
Module module = new SimpleModule("MyModule") {
@Override
public void setupModule(Module.SetupContext context) {
context.addAbstractTypeResolver(new SimpleAbstractTypeResolver().addMapping(GrantedAuthority.class, SimpleGrantedAuthority.class));
}
};
謝謝
編輯
我試過沙善的建議,所以我創建了這個類:
abstract class SimpleGrantedAuthorityMixIn {
@JsonCreator
SimpleGrantedAuthorityMixIn(@JsonProperty("authority") String authority) {
}
}
,然後我註冊它像這樣:
Module module = new SimpleModule("SamplestoreModule") {
@Override
public void setupModule(Module.SetupContext context) {
context.addAbstractTypeResolver(new SimpleAbstractTypeResolver().addMapping(GrantedAuthority.class, SimpleGrantedAuthority.class));
context.setMixInAnnotations(SimpleGrantedAuthority.class, SimpleGrantedAuthorityMixIn.class);
}
};
objectMapper.registerModule(module);
objectMapper.addMixIn(SimpleGrantedAuthority.class, SimpleGrantedAuthorityMixIn.class);
但我仍然看到相同的例外。
謝謝你 - 我已經試過了,但它不工作:( –
股票發行/異常 – Shashank
它應該工作,更新的代碼試試吧,你也需要DONOT背景下' .setMixInAnnotations(SimpleGrantedAuthority.class,SimpleGrantedAuthorityMixIn.class);'也告訴我你正在使用哪個版本的jaackon – Shashank