有关于如何使用 Spring 3.0 内容协商进行 RESTful 服务的示例吗?

发布于 2024-08-17 08:17:17 字数 2110 浏览 4 评论 0原文

我正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-08-24 08:17:17

我相信您的问题是 XML 的内容类型不是 text/xml,而是 application/xml。您会发现 MarshallingView 不接受 text/xml 内容类型。

ContentNegotiatingViewResolver 的作用是查阅其每个视图,询问它们是否接受从请求解析的内容类型。对于您想要支持的每种内容类型,您需要一个具有匹配 contentType 属性的视图。

您可以更改 mediaTypes 属性中的内容类型,也可以将 MarshallingViewcontentType 属性重写为 text/xml< /代码>。

I believe you're problem is that the content-type of XML is not text/xml, it's application/xml. You'll find that MarshallingView will not accept a content type of text/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 matching contentType property.

You can either change the content type in the mediaTypes property, or you can override contentType property of MarshallingView to be text/xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文