Spring JSON Response: 仅序列化响应对象内容(不包装 Root Value)
Spring JSON Response: Serialize only the response object content (do not Wrap Root Value)
在我的应用程序中,每个 restful 服务 returns 以下对象,指示请求是成功终止还是错误终止,它包含可选的错误消息,当然还有对象:
public class JSONResponse {
public boolean success = true;
public String errmsg = "";
public Object body;
/* getter/setter */
}
当我调用 restful 服务时,我得到以下 JSON:
{
"JSONResponse":{
"success":true,
"errmsg":"",
"body":[]
}
}
不幸的是,我已经有了客户端代码,它希望只获取对象内容,如下所示:
{
"success":true,
"errmsg":"",
"body":[]
}
我想保留我现有的 JS 代码。如何配置 MappingJackson2JsonView 以获取仅包含 "JSONResponse:" 字段内容的 JSON。
以下是简化的 restful 控制器的一部分:
@RequestMapping(value="/list")
public JSONResponse list() {
JSONResponse response = new JSONResponse();
response.success(new String[] { "a", "b", "c"});
return response;
}//EndMethod.
在 xml 中,我使用 MappingJackson2JsonView 默认视图作为部分附加 xml:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
像这样定义你自己的ObjectMapper
:
public class MyCustomMapper extends ObjectMapper {
public MyCustomMapper() {
//this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
//this.setSerializationInclusion(Include.NON_NULL);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
}
}
然后在你的 xml:
添加 bean 定义
<bean id="myCustomObjectMapper" class="put.your.package.name.MyCustomMapper"/>
最后重新配置 MappingJackson2HttpMessageConverter
:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="myCustomObjectMapper" />
</bean>
为了从生成的 JSON 响应中删除根密钥名称,我通过添加 p:extractValueFromSingleKeyModel="true"
更新了 spring 配置文件
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
p:extractValueFromSingleKeyModel="true" />
</list>
</property>
</bean>
我还更新了根 beans 标记以提供 "p",如以下代码片段所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
简要技术说明
用调试器做一些测试,我注意到 ObjectMapper
的方法 writeValue
接收到一个只有一项的 HashMap,其中键是 "JSONResponse",值是控制器方法返回的对象。因此,ObjectMapper 将从这个 HashMap 实例生成 JSON,因此 "JSONResponse" 作为根存在,因为序列化程序从哈希映射对象开始。
默认情况下,spring 中的 WRAP ROOT VALUE 值为 false,并且在特定情况下不影响 "JSONResponse" 字符串作为根的存在。事实上,通过启用它,您将获得 { "HashMap": { "JSONResponse": ... }}
。所以this.configure(SerializationFeature.WRAP_ROOT_VALUE, false)
没有解决问题。
MappingJackson2JsonView
有一个方法setExtractValueFromSingleKeyModel
,来自文档here“设置是否将包含单个属性的模型序列化为映射或是否提取来自模型的单个值并直接将其序列化”。默认为false,不从hashmap中提取值,但hashmap本身就是转换为JSON的对象。当这个 属性 设置为 true 时,它从哈希映射中提取值并将其转换为 JSON.
在我的应用程序中,每个 restful 服务 returns 以下对象,指示请求是成功终止还是错误终止,它包含可选的错误消息,当然还有对象:
public class JSONResponse {
public boolean success = true;
public String errmsg = "";
public Object body;
/* getter/setter */
}
当我调用 restful 服务时,我得到以下 JSON:
{
"JSONResponse":{
"success":true,
"errmsg":"",
"body":[]
}
}
不幸的是,我已经有了客户端代码,它希望只获取对象内容,如下所示:
{
"success":true,
"errmsg":"",
"body":[]
}
我想保留我现有的 JS 代码。如何配置 MappingJackson2JsonView 以获取仅包含 "JSONResponse:" 字段内容的 JSON。
以下是简化的 restful 控制器的一部分:
@RequestMapping(value="/list")
public JSONResponse list() {
JSONResponse response = new JSONResponse();
response.success(new String[] { "a", "b", "c"});
return response;
}//EndMethod.
在 xml 中,我使用 MappingJackson2JsonView 默认视图作为部分附加 xml:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
像这样定义你自己的ObjectMapper
:
public class MyCustomMapper extends ObjectMapper {
public MyCustomMapper() {
//this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
//this.setSerializationInclusion(Include.NON_NULL);
this.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
}
}
然后在你的 xml:
添加 bean 定义<bean id="myCustomObjectMapper" class="put.your.package.name.MyCustomMapper"/>
最后重新配置 MappingJackson2HttpMessageConverter
:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="myCustomObjectMapper" />
</bean>
为了从生成的 JSON 响应中删除根密钥名称,我通过添加 p:extractValueFromSingleKeyModel="true"
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
p:extractValueFromSingleKeyModel="true" />
</list>
</property>
</bean>
我还更新了根 beans 标记以提供 "p",如以下代码片段所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
简要技术说明
用调试器做一些测试,我注意到 ObjectMapper
的方法 writeValue
接收到一个只有一项的 HashMap,其中键是 "JSONResponse",值是控制器方法返回的对象。因此,ObjectMapper 将从这个 HashMap 实例生成 JSON,因此 "JSONResponse" 作为根存在,因为序列化程序从哈希映射对象开始。
默认情况下,spring 中的 WRAP ROOT VALUE 值为 false,并且在特定情况下不影响 "JSONResponse" 字符串作为根的存在。事实上,通过启用它,您将获得 { "HashMap": { "JSONResponse": ... }}
。所以this.configure(SerializationFeature.WRAP_ROOT_VALUE, false)
没有解决问题。
MappingJackson2JsonView
有一个方法setExtractValueFromSingleKeyModel
,来自文档here“设置是否将包含单个属性的模型序列化为映射或是否提取来自模型的单个值并直接将其序列化”。默认为false,不从hashmap中提取值,但hashmap本身就是转换为JSON的对象。当这个 属性 设置为 true 时,它从哈希映射中提取值并将其转换为 JSON.