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])发布于 2017-05-31 21:25:09
例外非常清楚地表明,GrantedAuthority不是一个具体的类(它是一个接口),而且映射程序只能使用具体的类,或者应该有一个针对GrantedAuthority的自定义序列化程序/反序列化器。一种解决方案是让UserAccountDetails使用SimpleGrantedAuthority而不是GrantedAuthority。
https://stackoverflow.com/questions/44294830
复制相似问题