Spring MVC mapping Guava Multimap

风格不统一 提交于 2021-01-28 19:07:19

问题


My controller can't map a Google Guava Multimap coming from the frontend. I send from my Javascript this object:

{1:[true,false], 2:[false,true], ...}. 

If I use a standard

java.util.Map<Long, List<Boolean>> 

everything works fine. But not with the Guava Multimap. Do I have to configure Spring to use some custom converter, or what is the problem?

The controller is:

@RequestMapping(path = "/myurl", method = RequestMethod.POST, produces = CotrollerKonstanten.JSON_UTF8)
public long myMethod(@RequestBody MappingDto mappingDto) {
  //...
}

My exception is:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: 
  Can not construct instance of com.google.common.collect.Multimap, problem:
  abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: java.io.PushbackInputStream@4b9c2db; line: 1, column: 13] (through reference chain: ...myClass); 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
  Can not construct instance of com.google.common.collect.Multimap, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

回答1:


Did you register the Guava module? By default, Jackson (and hence Spring) does not support serializing or deserializing to Guava datatypes.

The Guava module may or may not work for you depending on what implementation of Multimap you want -- not all datatypes are implemented.



来源:https://stackoverflow.com/questions/38323594/spring-mvc-mapping-guava-multimap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!