spring-mvc - Spring MVC 映射 Guava Multimap

标签 spring-mvc mapping guava multimap

我的 Controller 无法映射来自前端的 Google Guava Multimap。我从我的 Javascript 发送这个对象:

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

如果我使用标准

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

一切正常。但 Guava Multimap 则不然。我是否必须配置 Spring 才能使用某些自定义转换器,或者有什么问题?

Controller 是:

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

我的异常(exception)是:

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

最佳答案

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

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

关于spring-mvc - Spring MVC 映射 Guava Multimap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323594/

相关文章:

java - 抽象类和子类的构造函数注入(inject)

java - 两个@RequestMapping注解的区别

java - 从数据库中过滤数据的正确方法

java - Spring MVC 3.2.5 无法识别的字段 jackson

java - 递归映射时的 MapStruct 问题

java - Guava 库中的 Iterators.any

arraylist - 使用 ArrayList 作为 Java 中的值进行映射 - 为什么要使用第三方库?

python - 从其他数据集映射数据。 python Pandas

fluent-nhibernate - 如何在 Fluent NHibernate 中映射 IDictionary<string, object>?

java - Eclipse 无法编译通用代码,但 mvn compile 有效