简体   繁体   English

soap webservices - 返回错误消息?

[英]soap webservices - return error message?

I have a little soap webservice. 我有一个小肥皂网络服务。 And I wonder how I can response with errors. 我想知道我如何能够回答错误。

Eg: 例如:

public int createUser(String username) {
   //create...
   return id;
}

Now, if the user could not be created, how do I make a proper response? 现在,如果无法创建用户,我该如何做出正确的回复? Do I have to throw a custom exception? 我是否必须抛出自定义异常? Which is the right way? 哪种方法正确?

这里正确的方法是将异常转换为SOAP Fault

My experience with this is that we created a common class that gets returned along with the data class. 我的经验是,我们创建了一个与数据类一起返回的公共类。 So for example, the soap response comes back with a CommonOut object and a ResultData object. 因此,例如,soap响应返回CommonOut对象和ResultData对象。 The CommonOut class contains fields that are populated with data whether the request is good or bad such as Status, Records Returned, and Message. CommonOut类包含填充数据的字段,无论请求是好还是坏,例如Status,Records Returned和Message。

An interceptor is used to catch all exceptions in the container and wrap them in our custom format and returned to the user. 拦截器用于捕获容器中的所有异常,并以我们的自定义格式包装它们并返回给用户。 That way you can catch the real exception messages and wrap them in your own format. 这样你就可以捕获真正的异常消息并以自己的格式包装它们。

The danger of returning the real error messages to the caller is that it could expose details about your system potentially unless you catch exceptions and override the values that get returned. 将实际错误消息返回给调用者的危险在于它可能会暴露有关系统的详细信息,除非您捕获异常并覆盖返回的值。

This is similar to the SOAP fault mentioned above, but gives you more ability to customize your output format. 这类似于上面提到的SOAP错误,但是您可以更自如地定制输出格式。

我在我的SOAP服务器中执行此操作 - 但是不要试图构建自己的Soap错误 - 只是抛出一个运行时异常并且应该转换为soap错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM