Spring MVC映射Guava Multimap

时间:2016-07-12 08:27:18

标签: spring-mvc mapping guava multimap

我的控制器无法映射来自前端的Google Guava Multimap。我从我的Javascript发送此对象:

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

如果我使用标准

java.util.Map<Long, List<Boolean>> 
一切正常。但不是Guava Multimap。我是否必须将Spring配置为使用某些自定义转换器,或者是什么问题?

控制器是:

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

我的例外是:

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 个答案:

答案 0 :(得分:3)

你有register the Guava module吗?默认情况下,Jackson(以及Spring)不支持序列化或反序列化为Guava数据类型。

Guava模块可能适用于您,也可能不适合您,具体取决于您想要的Multimap实现方式 - 并非所有数据类型都已实现。