一,先说明下为啥使用multimap这种数据结构
前端需要返回各地区的门店信息,会出现一个key下有多个value,类似:
{
"data": {
"上海": [
{
"id": 4358,
"venderId": 200118,
"cityName": "徐汇区",
"storeNumbers": 1
},
{
"id": 2668,
"venderId": 200118,
"cityName": "虹口区",
"storeNumbers": 3
}
],
"北京": [
{
"id": 2664,
"venderId": 200118,
"cityName": "昌平区",
"storeNumbers": 1
},
{
"id": 859,
"venderId": 200118,
"cityName": "朝阳区",
"storeNumbers": 3477
}
]
}
}
二,出现的问题:
如果不加任何处理,在controller里直接返回这种数据结构会出现:
{
"fields":{
"empty": false
}
}
三,原因:
spring不能正确的序列化这种类型的数据结构
四,处理方法:
要手动向jackson的ObjectMapper中注册一个Module
1,添加mvn依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.2.0</version>
</dependency>
<!-- JSON parser configuration-->
<bean id="guavaObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref bean="guavaObjectMapper" /></property>
<property name="targetMethod"><value>registerModule</value></property>
<property name="arguments">
<list>
<bean id="guavaModule" class="com.fasterxml.jackson.datatype.guava.GuavaModule"/>
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<ref bean="guavaObjectMapper"/>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>