首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >org.springframework.http.converter.HttpMessageNotReadableException

org.springframework.http.converter.HttpMessageNotReadableException
EN

Stack Overflow用户
提问于 2017-05-31 21:17:39
回答 1查看 2.9K关注 0票数 0
代码语言:javascript
复制
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;
    }

我已经在上面设置了我的示例对象和下面的测试。

代码语言:javascript
复制
@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(),但可能我的实现是错误的。任何帮助都将不胜感激。谢谢!

代码语言:javascript
复制
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给出的答案,我得到了这个新的错误

代码语言:javascript
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2017-05-31 21:25:09

例外非常清楚地表明,GrantedAuthority不是一个具体的类(它是一个接口),而且映射程序只能使用具体的类,或者应该有一个针对GrantedAuthority的自定义序列化程序/反序列化器。一种解决方案是让UserAccountDetails使用SimpleGrantedAuthority而不是GrantedAuthority。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44294830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档