我的 Spring-MVC ContentNegotiatingViewResolver 设置正确吗?如何针对不支持的媒体类型发送 404 错误?

发布于 2024-12-17 17:00:29 字数 1903 浏览 2 评论 0原文

我不知道这对于这个网站来说是否是一个有效的问题,但我想知道是否有使用过 ContentNegotiatingViewResolver 的人可以查看一下这个问题,让我知道我的设置是否正确,并帮助我发送 404 消息。

我想做的是将所有没有扩展名的 url 默认为 HTML 表示形式(在我的例子中是 freemarker 视图)。我想接受附加“.json”的网址来渲染 json。这似乎适用于 Firefox、IE 和 Chrome。我猜它在其他浏览器中也有效?我确保禁用接受标头,因为它是一个邪恶的功能,并不像文档所说的那样真正起作用。

我尝试使用“.stuff”访问网址,只是为了看看会发生什么,并且根据我的配置,出现了空白屏幕。这是可以接受的吗?有什么办法可以发送 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>

I dunno if this is a valid question for this site, but I was wondering if someone experienced with the ContentNegotiatingViewResolver could look this over and let me know if I set it up correctly and well as help me send 404 messages.

What I'd like to do is make all urls with no extension default to the HTML representation (which is freemarker views in my case). I'd like to accept urls with ".json" appended to them to render json instead. This appears to work in firefox, ie and chrome. I'm guessing it works in other browsers? I made sure to disable the accept header because it's an evil feature that doesn't really work like the documentation says it does.

I have tried to access urls with ".stuff", just to see what happens, and with my configuration, a blank screen happens. Is this acceptable? Is there any way I can send a 404 error?

Is there anything else that I may have not configured properly?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柠檬心 2024-12-24 17:00:29

因为您设置了 defaultContentType,所以协商最终总是会找到 freemarker 视图提供的匹配内容类型。引用 ContentNegotiatingViewResolver 的 javadoc:

您也可以直接设置setDefaultContentType,这将是
当其他机制(接受标头、文件扩展名或
参数)不会导致匹配。

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

然后,使用 useNotAcceptableStatusCode:

如果没有匹配到,将返回406(Not Acceptable)状态码
找到了。

我刚刚尝试了这个(使用另一个 REST 服务应用程序的设置)并看到 Chrome 显示消息:此请求标识的资源只能生成具有根据请求“accept” headers() 不可接受的特征的响应。

Because you have defaultContentType set, the negotiation always ends up finding a matching content type delivered by the freemarker view. A quote from the javadoc of ContentNegotiatingViewResolver:

You can also set the setDefaultContentType directly, which will be
returned when the other mechanisms (Accept header, file extension or
parameter) do not result in a match.

With this setting, file extension .stuff matches contentType text/html.

Then, with useNotAcceptableStatusCode:

406 (Not Acceptable) status code will be returned if no match is
found.

I just tried this (with the settings of another REST service app) and saw Chrome showing the message: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers().

北斗星光 2024-12-24 17:00:29

您是否将“.stuff”的 url-pattern 添加到 web.xml 中?我正在使用 PathExtensionContentNegotiationStrategy,但应该是同样的原因。因为 spring servlet 无法响应此请求,所以得到 404 错误,而不是 500 或 416。如果是 416,则应该导致某些标头,可以通过更改 jQuery seeting 或 http 客户端标头来修复。

Did you add url-pattern for ".stuff" to web.xml? I am using PathExtensionContentNegotiationStrategy, but should be same reason. Because spring servlet can not response this request, so got 404 error, not 500 or 416. If is 416, it should caused some header, can fixed by change jQuery seeting or http client header.

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