我不知道这是否是该站点的有效问题,但我想知道是否有使用ContentNegotiatingViewResolver的人可以查看一下,并告诉我是否设置正确并帮助我发送404消息。

我想做的是将所有不带扩展名的url默认为HTML表示形式(在我的情况下是freemarker View )。我想接受附加了“.json”的网址来呈现json。这似乎可以在Firefox(即chrome)中使用。我猜它可以在其他浏览器中使用吗?我确保禁用accept header ,因为它是一个邪恶的功能,实际上并没有像文档中所说的那样起作用。

我试图使用“.stuff”访问url,只是为了查看会发生什么,并且在我的配置下,出现了黑屏。这可以接受吗?有什么办法可以发送404错误?

还有其他我可能配置不正确的问题吗?

<bean id="contentNegotiatingViewResolver"
      class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1"/>
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="text/html" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="useNotAcceptableStatusCode" value="true" />
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="contentType" value="application/json" />
            </bean>
        </list>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                <property name="contentType" value="text/html" />
                <property name="order" value="2"/>
                <property name="cache" value="true"/>
                <property name="prefix" value=""/>
                <property name="suffix" value=".ftl"/>
                <property name="exposeSpringMacroHelpers" value="true"/>
            </bean>
        </list>
    </property>
</bean>

最佳答案

因为您设置了defaultContentType,所以协商总是最终找到freemarker View 提供的匹配内容类型。 ContentNegotiatingViewResolver的javadoc中的一句话:



使用此设置,文件扩展名.stuff与contentType text/html匹配。

然后,使用useNotAcceptableStatusCode:



我只是尝试了这一点(使用另一个REST服务应用程序的设置),然后看到Chrome显示消息:此请求标识的资源只能根据请求“accept” headers()生成特性 Not Acceptable 响应。

09-27 12:32