【问题标题】:Spring MVC mapping Guava MultimapSpring MVC 映射 Guava Multimap
【发布时间】:2016-07-12 08:27:18
【问题描述】:

我的控制器无法映射来自前端的 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

【问题讨论】:

  • 请更具体一点:向我们展示您的代码(控制器、转换器)以及您遇到的异常情况。

标签: spring-mvc mapping guava multimap


【解决方案1】:

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

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

【讨论】:

  • 啊,我想这就是原因。我的 pom 中没有该模块。我认为它可以开箱即用 :-) 谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-04-30
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多