0
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;
}
我已經在下面的測試中設置了我的示例對象。org.springframework.http.converter.HttpMessageNotReadableException
@Test
public void checkCreateUsers() throws Exception{
List<UserAccountDetails> detailsList = getUserAccountDetails();
String jsonObject = mapper.writeValueAsString(detailsList);
System.out.println(jsonObject);
mockMvc.perform(post("https://stackoverflow.com/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: [email protected]; 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: [email protected]; 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: [email protected]; 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: [email protected]; line: 1, column: 116] (through reference chain: java.util.ArrayList[0]->com.sammy.domain.UserAccountDetails["authorityList"]->java.util.ArrayList[0])
這也行不通,我已經完成並更新了新的錯誤。 – Sammy65