public static List<UserAccountDetails> getUserAccountDetails() {
List<UserAccountDetails> detailsList = new ArrayList<>();
List<GrantedAuthority> authorities = getAuthorityList();
UserAccountDetails accountDetail = UserAccountDetails.builder()
.firstName("People")
.lastName("Person")
.username("pperson")
.password("who")
.authorityList(authorities)
.build();
detailsList.add(getUserDetails());
detailsList.add(accountDetail);
return detailsList;
}
private static List<GrantedAuthority> getAuthorityList() {
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
return authorities;
}
我已通过下面的测试设置了上面的示例对象。
@Test
public void checkCreateUsers() throws Exception{
List<UserAccountDetails> detailsList = getUserAccountDetails();
String jsonObject = mapper.writeValueAsString(detailsList);
System.out.println(jsonObject);
mockMvc.perform(post("/users/addUsers")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonObject))
.andDo(print())
.andExpect(status().isCreated());
}
我的测试失败,因为ObjectMapper会抛出下面的错误。我可以看到示例数据已正确序列化然后使用mapper.registerSubtypes()
尝试但可能我的实现是错误的。任何帮助将不胜感激。感谢!!!
WARN 15972 --- [ main] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Can not construct instance of org.springframework.security.core.GrantedAuthority: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: java.io.PushbackInputStream@6242ae3b; line: 1, column: 115] (through reference chain: java.util.ArrayList[0]->com.sammy.domain.UserAccountDetails["authorityList"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.security.core.GrantedAuthority: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: java.io.PushbackInputStream@6242ae3b; line: 1, column: 115] (through reference chain: java.util.ArrayList[0]->com.sammy.domain.UserAccountDetails["authorityList"]->java.util.ArrayList[0])
我根据@tsolakp给出的答案得到了这个新错误
2017-06-01 11:42:12.940 WARN 28663 --- [ main] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: 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: java.io.PushbackInputStream@46a488c2; line: 1, column: 116] (through reference chain: java.util.ArrayList[0]->com.sammy.domain.UserAccountDetails["authorityList"]->java.util.ArrayList[0]); nested exception is 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: java.io.PushbackInputStream@46a488c2; line: 1, column: 116] (through reference chain: java.util.ArrayList[0]->com.sammy.domain.UserAccountDetails["authorityList"]->java.util.ArrayList[0])
答案 0 :(得分:0)
很明显的例外是 GrantedAuthority 不是具体的类(它是一个接口),mapper只适用于具体的类,或者应该有GrantedAuthority的自定义序列化器/反序列化器。一种解决方案是使用 SimpleGrantedAuthority 而不是 GrantedAuthority 来 UserAccountDetails 。