我正在尝试将 Spring MVC 3.2 异步延迟结果用于 RESTful JSON 服务。
使用同步方法:
@ResponseBody
@RequestMapping(value = "/catalog", method = RequestMethod.GET, produces = "application/json")
public Entry catalog() {
Entry entry = new Entry();
entry.timestamp = System.currentTimeMillis();
entry.summary = "Hello World!";
entry.body = new HashMap<String, Object>();
entry.body.put("key1", "value1");
entry.body.put("key2", "value2");
entry.body.put("key3", "value3");
return entry;
}
我得到以下 JSON 结果:
{"timestamp":1359721240340,"summary":"Hello World!","body":{"key3":"value3","key2":"value2","key1":"value1"}}
使用异步方法(人为示例):
@ResponseBody
@RequestMapping(value = "/catalogs", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<Entry> catalogs() {
Entry entry = new Entry();
entry.timestamp = System.currentTimeMillis();
entry.summary = "Hello World!";
entry.body = new HashMap<String, Object>();
entry.body.put("key1", "value1");
entry.body.put("key2", "value2");
entry.body.put("key3", "value3");
DeferredResult<Entry> result = new DeferredResult<Entry>();
result.setResult(entry);
return result;
}
我得到以下信息:
{}{"timestamp":1359721240340,"summary":"Hello World!","body":{"key3":"value3","key2":"value2","key1":"value1"}}
那么前缀空对象(即{})是怎么回事?我是在做一些明显愚蠢的事情,还是MappingJackson2JsonView
不能很好地使用新的异步功能?
仅供参考,这是我的 Spring MVC bean 配置:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager" />
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>