如何制作Tomcat 6服务器JSON内容

发布于 2024-12-28 10:43:00 字数 680 浏览 1 评论 0 原文

当我尝试使用 GET 请求请求不存在的资源时 -

http://localhost:8080/jackrabbit/server/default/jcr:root/user/abc/article/article-1.0.json

我期待 404 响应,但在JSON 格式。但我得到的是 html 响应 -

<html><head><title>Apache Tomcat/6.0.32 - Error report</title>...</body></html>

如何确保此内容以 JSON 格式而不是 HTML 格式返回。我在 web.xml 中指定了以下内容,但这没有帮助 -

<mime-mapping>
    <extension>json</extension>
    <mime-type>application/json</mime-type>
</mime-mapping>

When I try to request for a non existent resource using a GET request for -

http://localhost:8080/jackrabbit/server/default/jcr:root/user/abc/article/article-1.0.json

I am expecting a 404 response but in JSON format. But instead I get an html response -

<html><head><title>Apache Tomcat/6.0.32 - Error report</title>...</body></html>

How can I make sure that this content is returned in JSON format instead of HTML format. I have specified the following in web.xml but that did not help -

<mime-mapping>
    <extension>json</extension>
    <mime-type>application/json</mime-type>
</mime-mapping>

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

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

发布评论

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

评论(1

浅蓝的眸勾画不出的柔情 2025-01-04 10:43:00

您应该配置一个 404 错误页面来设置正确的内容类型并输出 JSON。

<error-page>
    <error-code>404</error-code>
    <location>/notFoundJSON</location>
</error-page>

其中 /notFoundJSON 映射到使用 response.setContentType(..) 的 servlet,并将 JSON 写入 response.getWriter()

You should configure a 404 error page that sets the proper content type and outputs JSON.

<error-page>
    <error-code>404</error-code>
    <location>/notFoundJSON</location>
</error-page>

Where /notFoundJSON is mapped to a servlet that uses response.setContentType(..) and writes JSON to the response.getWriter()

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