java Spring MappingJacksonJsonView,如何告诉使用它而不是 JSP 视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6794974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MappingHymansonJsonView, how to tell to use it instead of JSP view?
提问by stivlo
I'm trying to use MappingHymansonJsonView
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 MappingHymansonJsonView
to render a request. I tried to use the same name for view name and bean name of MappingHymansonView
, but didn't work. I built a sample test application here: https://github.com/stivlo/restjson
我正在尝试MappingHymansonJsonView
与 Spring 3.0一起使用,但没有成功。我不知道我做错了什么,我认为问题是我不知道如何使用MappingHymansonJsonView
来呈现请求。我尝试对 的视图名称和 bean 名称使用相同的名称MappingHymansonView
,但没有奏效。我在这里构建了一个示例测试应用程序:https: //github.com/stivlo/restjson
In web.xmlI've defined ContextLoaderListener
and the mapping for dispatcherServlet
.
在web.xml 中,我定义ContextLoaderListener
了dispatcherServlet
.
In servlet-context.xmlI've added
<mvc:annotation-driven/>
and
和
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingHymansonJsonView"/>
In org.obliquid.restjson.web.ToDoList.javaI set the logical view name as jsonView
.
在org.obliquid.restjson.web.ToDoList.java 中,我将逻辑视图名称设置为jsonView
.
However, instead of using MappingHymansonJsonView
, it looks for a JSP file, according to my JSP mapping.
但是,它不是使用MappingHymansonJsonView
,而是根据我的 JSP 映射查找 JSP 文件。
message /restjson/WEB-INF/jsp/jsonView.jsp
description The requested resource (/restjson/WEB-INF/jsp/jsonView.jsp)
is not available.
What should I change to use MappingHymansonJsonView
as a renderer?
我应该更改什么以MappingHymansonJsonView
用作渲染器?
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.
更新 1:在以下测试中,我发现如果我将以下内容添加到我的 servlet-context.xml 中,JSON 呈现有效,但我呈现为 JSP(主页)的其他视图不再工作。
<!-- Resolve views based on string names -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
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:
更新 2:我删除BeanNameViewResolver
并更改了我的 ToDoList.java 以直接返回要在 JSON 中转换的集合,而不是 ModelAndView,并带有@ResponseBody
注释,如下所示:
@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;
}
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?
以这种方式工作。即使映射更加“神奇”。这让我想知道,例如,如果 XML 存在类似的渲染器,Spring 怎么知道选择哪个渲染器?
采纳答案by Tomasz Nurkiewicz
Spring will use Accept
header sent by the client to return most appropriate view. Hereyou will find my complete Spring MVC application that returns both JSON and XML.
Spring 将使用Accept
客户端发送的标头来返回最合适的视图。在这里,您将找到我的完整 Spring MVC 应用程序,它返回 JSON 和 XML。
As you can see, I only needed:
如您所见,我只需要:
<mvc:annotation-driven />
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.
我还使用了相同的注释:@RequestMapping
将请求映射到方法并@ResponseBody
告诉 Spring 我从控制器返回的是实际响应。然而,它可能需要一些调整/格式化,这里 Spring 负责将您的对象编组为最合适的类型,如 JSON。
回答by danny.lesnik
You should do it this way:
你应该这样做:
In your xml file set the following: set
在您的 xml 文件中设置以下内容:
<mvc:annotation-driven />
After it you need to set Hymanson serializer:
之后,您需要设置 Hymanson 序列化程序:
<bean id="HymansonMessageConverter" class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="HymansonMessageConverter"/>
</list>
</property>
</bean>
after it you can use it in your Controller:
之后你可以在你的控制器中使用它:
@RequestMapping(value="/getObjects",method = RequestMethod.POST)
@ResponseBody
public List<MyObject> getCategories(){
List<MyObject> objects = daoService.gettAllObjects();
return objects;
}
回答by Dikshit Rajkhowa
Adding the following worked in my case
在我的情况下添加以下内容
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0" />
</bean>
So basically we should try to resolve any view as a bean first
所以基本上我们应该首先尝试将任何视图解析为 bean
回答by liximomo
you will need to see ContentNegotiatingViewResolver,and set defaultviewsproperty to MappingHymansonJsonView, and @ResponseBody uses HttpMessageConverter to instead of ViewSolver,see the differences between them http://ufasoli.blogspot.com/2013/08/viewresolver-vs-messageconverter-spring.html
您需要查看ContentNegotiatingViewResolver,并将defaultviews属性设置为MappingHymansonJsonView,@ResponseBody 使用 HttpMessageConverter 代替 ViewSolver,查看它们之间的差异 http://ufasoli.blogspot.com/2013/08/viewresolver-vs-messageconverter-spring .html