![](/img/trans.png)
[英]Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException
[英]org.springframework.http.converter.HttpMessageNotReadableException
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])
非常明顯的例外是, GrantedAuthority不是具體的類(它是一個接口),並且映射器僅適用於具體的類,或者應該為GrantedAuthority使用自定義的序列化器/反序列化器。 一種解決方案是讓UserAccountDetails使用SimpleGrantedAuthority而不是GrantedAuthority 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.