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;
}
我已经通过下面的测试设置了上面的示例对象.
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());
}
我的测试失败了,因为 ObjectMapper 抛出了下面的错误.我可以看到示例数据已正确序列化,然后尝试使用 mapper.registerSubtypes()
但也许我的实现是错误的.任何帮助将不胜感激.谢谢!!!
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])
根据@tsolakp 给我的答案,我收到了这个新错误
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])
推荐答案
例外很清楚地表明 GrantedAuthority 不是一个具体的类(它是一个接口)并且映射器只适用于具体的类或者应该有一个用于 GrantedAuthority 的自定义序列化器/反序列化器.一种解决方案是让 UserAccountDetails 使用 SimpleGrantedAuthority 而不是 GrantedAuthority.
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.
这篇关于org.springframework.http.converter.HttpMessageNotReadableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!