Spring MappingJacksonJsonView,如何分辨使用它而不是JSP视图? [英] Spring MappingJacksonJsonView, how to tell to use it instead of JSP view?
问题描述
我正在尝试在Spring 3.0中使用MappingJacksonJsonView
,但没有成功.我不知道我在做什么错,我认为问题是我不知道如何告诉使用MappingJacksonJsonView
来呈现请求.我试图对MappingJacksonView
的视图名称和bean名称使用相同的名称,但是没有用.我在这里构建了一个示例测试应用程序: https://github.com/stivlo/restjson
I'm trying to use MappingJacksonJsonView
with Spring 3.0, without success. I don't know what I'm doing wrong, I think the problem is that I don't know how to tell to use the MappingJacksonJsonView
to render a request. I tried to use the same name for view name and bean name of MappingJacksonView
, but didn't work. I built a sample test application here: https://github.com/stivlo/restjson
在 web.xml 我已经定义了ContextLoaderListener
和dispatcherServlet
的映射.
In web.xml I've defined ContextLoaderListener
and the mapping for dispatcherServlet
.
在 servlet上下文中.xml 我添加了
<mvc:annotation-driven/>
和
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
在 org中.obliquid.restjson.web.ToDoList.java 我将逻辑视图名称设置为jsonView
.
In org.obliquid.restjson.web.ToDoList.java I set the logical view name as jsonView
.
但是,根据我的JSP映射,它不是使用MappingJacksonJsonView
,而是查找JSP文件.
However, instead of using MappingJacksonJsonView
, it looks for a JSP file, according to my JSP mapping.
message /restjson/WEB-INF/jsp/jsonView.jsp
description The requested resource (/restjson/WEB-INF/jsp/jsonView.jsp)
is not available.
使用MappingJacksonJsonView
作为渲染器应该改变什么?
What should I change to use MappingJacksonJsonView
as a renderer?
更新1 :在以下测试中,我发现,如果将以下内容添加到servlet-context.xml中,则JSON呈现有效,但其他视图(呈现为JSP(主页))不再工作.
UPDATE 1: In following tests I've found that if I add the following to my servlet-context.xml, JSON rendering works, but my other view, rendered as JSP (home) is not working anymore.
<!-- Resolve views based on string names -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
更新2 :我删除了BeanNameViewResolver
并更改了ToDoList.java以直接返回要转换为JSON的Collection,而不是带有@ResponseBody
注释的ModelAndView,如下所示:
UPDATE 2: I removed the BeanNameViewResolver
and changed my ToDoList.java to return directly the Collection to be converted in JSON, instead of ModelAndView, with a @ResponseBody
annotation, as follows:
@RequestMapping("/toDoList")
public @ResponseBody List<ToDoItem> test() {
List<ToDoItem> toDoList = new ArrayList<ToDoItem>();
toDoList.add(new ToDoItem(1, "First thing, first"));
toDoList.add(new ToDoItem(1, "After that, do the second task"));
return toDoList;
}
以这种方式起作用.即使映射更加神奇".我想知道,例如,如果存在用于XML的类似渲染器,Spring如何知道要选择哪个渲染器?
In this way it works. Even though the mapping is even more "magical". It makes me wonder, if a similar renderer exists for XML for instance, how does Spring know which renderer to pick?
推荐答案
Spring将使用客户端发送的Accept
标头返回最合适的视图. 这里,您将找到我完整的Spring MVC应用程序,该应用程序同时返回JSON和XML.
Spring will use Accept
header sent by the client to return most appropriate view. Here you will find my complete Spring MVC application that returns both JSON and XML.
如您所见,我只需要:
<mvc:annotation-driven />
我还使用了相同的批注:@RequestMapping
将请求映射到方法,而@ResponseBody
告诉Spring我从控制器返回的是实际响应.但是,它可能需要一些调整/格式化,在这里Spring负责将您的对象编组为最合适的类型,例如JSON.
I also used the same annotations: @RequestMapping
to map request to a method and @ResponseBody
to tell Spring that what I am returning from the controller is the actual response. It might however need some tweaking/formatting, and here Spring takes care of marshalling your object into most appropriate type like JSON.
这篇关于Spring MappingJacksonJsonView,如何分辨使用它而不是JSP视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!