org.springframework.http.converter.HttpMessageNotR

2019-08-22 10:38发布

问题:

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;
    }

I have setup my sample object above with the test below.

@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());
    }

My test fails because ObjectMapper throws the error below. I can see the sample data is properly serialized then tried using mapper.registerSubtypes() but maybe my implementation is wrong. Any help will be appreciated. Thanks!!!

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])

I get this new error based on the answer given me by @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])

回答1:

The exception is pretty clearly says that GrantedAuthority is not a concrete class (it is an interface) and mapper only works with concrete classes or should have a custom serializer/deserializer for GrantedAuthority. One solution is to have UserAccountDetails using SimpleGrantedAuthority instead of GrantedAuthority.