在不更改WSDL文件的情况下添加肥皂故障? [英] Adding soap faults without changing WSDL file?

查看:96
本文介绍了在不更改WSDL文件的情况下添加肥皂故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的JAX-WS Web服务中添加一些SOAP Fault,但是我不想更改WSDL模式的内容.据我所读,我将必须使用批注@WebFault来定义SOAP Fault,并且它会在下一次构建期间自动导致WSDL中的更改.有什么方法可以使用SOAP Faults而无需更改WSDL方案的内容?

I would like to add some SOAP Faults to my JAX-WS web service, but I would like not to change content of WSDL schema. As far as I read I would have to use annotation @WebFault to define SOAP Fault and it automatically causes changes in WSDL during next build. Is there any way to use SOAP Faults without changing content of WSDL scheme?

推荐答案

当您从代码中引发异常时,JAX-WS将在响应中将其自动映射到SOAP错误.无需在WSDL中定义错误.

When you throw an Exception from your code it will be automatically mapped to a SOAP fault by JAX-WS in the response. There's no need to define a fault in your WSDL.

使用@WebFault或在WSDL文件中定义<soap:fault>元素用于声明特定操作可能返回自定义SOAP错误.

Using @WebFault or defining a <soap:fault> element in the WSDL file is used to declare that a specific operation might return a custom SOAP fault.

@WebFault肯定会在结果WSDL中添加一个<soap:fault>元素.

@WebFault will definitely add a <soap:fault> element in the resulting WSDL.

回顾一下,抛出一个异常将在soap响应中插入一个元素.

To recap, throwing an Exception will insert a element in the soap response.

更新

自定义错误字符串:

作为参数传递给Exception构造函数的字符串消息表示<soap:fault>中的faultstring元素.示例:

The string message that you pass as a parameter to the Exception constructor represents the faultstring element in the <soap:fault>. Example:

throw new Exception("This is the faultstring text");

肥皂响应中导致的错误:

Resulting fault in the soap response:

<soap:fault>
    <faultcode>soap:Server</faultcode>
    <faultstring>This is the faultstring text</faultstring>
</soap:fault>

自定义故障代码:

我认为您不能使用普通的Java异常来更改故障代码.如果确实需要这样做,可以看一下JAX-WS

I don't think you can change the faultcode with normal Java exceptions. If you really need to do that you can take a look at JAX-WS SOAPFaultException.

请记住,故障代码用于指示产生的错误的类型,并且在大多数情况下,您将从网络服务中返回服务器故障.

Keep in mind that faultcodes are used to indicate the type of error produced, and most of the time you are going to return a Server fault from your webservice.

这是SOAP 1.1和1.2中存在的四个故障代码:

This are the four existing fault codes in SOAP 1.1 and 1.2:

  • VersionMismatch :为SOAP Envelope元素找到了无效的命名空间.
  • MustUnderstand :无法理解Header元素的直接子元素,其mustUnderstand属性设置为"1".
  • 客户端:邮件格式不正确或包含错误信息.
  • 服务器:服务器出现问题,因此消息无法继续.
  • VersionMismatch: Found an invalid namespace for the SOAP Envelope element.
  • MustUnderstand: An immediate child element of the Header element, with the mustUnderstand attribute set to "1", was not understood.
  • Client: The message was incorrectly formed or contained incorrect information.
  • Server: There was a problem with the server so the message could not proceed.

前三个将由JAX-WS在解析SOAP请求时创建,除非有非常特殊的情况或者您正在编写自己的JAX-WS处理程序/拦截器,否则您将不需要返回其他任何错误代码.服务器".

The first three are going to be created by JAX-WS when parsing the SOAP request and unless there's a very specific situation or you are writing your own JAX-WS handlers/interceptors you will not need to return any other faultcode besides 'Server'.

自定义详细信息:

<detail>元素将填充一个表示Exception的元素.例如,如果您抛出new MyCustomException("custom message"),它将是这样的:

The <detail> element will be populated with an element representing the Exception. For example if you are throwing a new MyCustomException("custom message") it will be something like this:

<detail>
    <MyCustomException>
        <message>custom message</message>
    </MyCustomException>
</detail>

这篇关于在不更改WSDL文件的情况下添加肥皂故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆