gpt4 book ai didi

java - 如何在 Web 服务中返回 HTTP 代码

转载 作者:行者123 更新时间:2023-12-01 23:29:32 25 4
gpt4 key购买 nike

我有一个用 Java 构建的 SOAP Web 服务。

如果我的方法遇到异常,我想返回“HTTP CODE 500”。

这可能吗?如果是的话怎么办?

(Web 服务在 Tomcat 6 上运行)

最佳答案

也许您应该简单地自己抛出一个合格的异常,然后该异常将作为 SOAP 错误发送回客户端。W3C 告诉我们这一点:

In case of a SOAP error while processing the request, the SOAP HTTP server MUST issue an HTTP 500 "Internal Server Error" response and include a SOAP message in the response containing a SOAP Fault element (see section 4.4) indicating the SOAP processing error. http://www.w3.org/TR/2000/NOTE-SOAP-20000508/

弄乱 http 响应代码可能很危险,因为其他一些客户端可能会期望不同的响应。在您的情况下,您会很幸运,因为您想要的行为正是 W3C 指定的。所以抛出异常;)

如何做到这一点?看看这里: How to throw a custom fault on a JAX-WS web service?

问候

巴斯蒂安

关于java - 如何在 Web 服务中返回 HTTP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com