问题
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