2017-05-31 45 views
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]) 

回答

0

唯一的例外是很清楚的說,的GrantedAuthority不是一個具體類(它是一個接口)和mapper只能用於具體的類,或者應該有一個用於GrantedAuthority的自定義序列化器/反序列化器。一種解決方案是UserAccountDetails使用SimpleGrantedAuthority而不是GrantedAuthority

+0

這也行不通,我已經完成並更新了新的錯誤。 – Sammy65

相關問題