有关于如何使用 Spring 3.0 内容协商进行 RESTful 服务的示例吗?
我正在阅读 Spring 3.0文档和博客文章(后续) 关于如何使用 Spring MVC 创建 REST 风格的服务,但我找不到任何有关如何使用 ContentNegotiatingViewResolver 的工作示例。我有一个像这样的测试控制器
@Controller
public class IndexController implements Serializable
{
@RequestMapping("/index")
public ModelAndView index()
{
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
}
,并尝试使用类似的东西
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="text/xml" />
<!--
<entry key="json" value="application/json"/>
-->
</map>
</property>
<property name="defaultContentType"><value>text/html</value></property>
<property name="defaultViews">
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</property>
</bean>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
</bean>
尝试根据 URL 中的扩展名解析视图(我想支持 html、.xml 和 .json)。 .html 视图有效(也显示了正确的 JSP 视图),但我尝试的其他设置和运行的 JSON 和 XML 似乎都不起作用(设置 defaultViews 属性只是我尝试过的事情之一)。似乎也没有那么多阅读材料。有人有经验或例子吗?
I was reading the Spring 3.0 Documentation and Blog Posts (followups) on how to create REST style services with Spring MVC but I can't find any working example on how to use the ContentNegotiatingViewResolver. I have a test controller like this
@Controller
public class IndexController implements Serializable
{
@RequestMapping("/index")
public ModelAndView index()
{
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
return mav;
}
}
and tried to use something like this
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="text/xml" />
<!--
<entry key="json" value="application/json"/>
-->
</map>
</property>
<property name="defaultContentType"><value>text/html</value></property>
<property name="defaultViews">
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</property>
</bean>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
</bean>
trying to resolve the views according to the extension in the URL (I want to support html, .xml and .json). The .html view works (showing the correct JSP view, too) but nothing else I tried for getting JSON and XML up and running seems to work (setting the defaultViews property was just one of the things I tried). There doesn't seem to be that much reading material either. Does anybody have experience or examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的问题是 XML 的内容类型不是
text/xml
,而是application/xml
。您会发现MarshallingView
不接受text/xml
内容类型。ContentNegotiatingViewResolver
的作用是查阅其每个视图,询问它们是否接受从请求解析的内容类型。对于您想要支持的每种内容类型,您需要一个具有匹配contentType
属性的视图。您可以更改
mediaTypes
属性中的内容类型,也可以将MarshallingView
的contentType
属性重写为text/xml< /代码>。
I believe you're problem is that the content-type of XML is not
text/xml
, it'sapplication/xml
. You'll find thatMarshallingView
will not accept a content type oftext/xml
.What
ContentNegotiatingViewResolver
does is consult each of its views, asking them if they'll accept the content type that was resolved from the request. For each content type you want to support, you need a view with a matchingcontentType
property.You can either change the content type in the
mediaTypes
property, or you can overridecontentType
property ofMarshallingView
to betext/xml
.