简体   繁体   English

SOAP 故障<detail>在 http 回复中不可见</detail>

[英]SOAP Faults <detail> in http response not visible

I have one issue.我有一个问题。 I've generated a WS server from a wsdl and i'm am trying to understand the exceptions handiling.我已经从 wsdl 生成了一个 WS 服务器,我正在尝试了解异常处理。 Basically in the WSDL is defined a custom exception called Throwable_Exception.基本上在 WSDL 中定义了一个名为 Throwable_Exception 的自定义异常。 Now I've tired to test the fault in the SOAP response and it work and thats the result:现在我已经厌倦了测试 SOAP 响应中的故障,它起作用了,这就是结果:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>TEST Custom Exception</faultstring>
      </soap:Fault>
   </soap:Body>
 </soap:Envelope>

The text in the "faultstring" tag is the messagge that i've thrown in the exception. “faultstring”标签中的文本是我在异常中抛出的消息。 Now my goal its to send in the response this kind of informations:现在我的目标是在响应中发送此类信息:

  1. A readable messagge about the exception关于异常的可读消息
  2. The exception thrown from the server服务器抛出的异常
  3. eventually the details about the exception最终有关异常的详细信息

I've seen on an guide from Oracle that the SOAP fault envelope should be like this:我在 Oracle 的指南中看到 SOAP 故障包络应该是这样的:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>Your name is required.</faultstring>
         <detail>
            <ns2:MissingName xmlns:ns2="http://examples/">
               <message>Your name is required.</message>
            </ns2:MissingName>
            <ns2:exception xmlns:ns2="http://jax-ws.dev.java.net/"
 class="examples.MissingName" note="To disable this feature, set
 com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system 
 property to false">
               <message>Your name is required.</message>
               <ns2:stackTrace>
                  <ns2:frame class="examples.HelloWorld" file="HelloWorld.java" 
  line="14" method="sayHelloWorld"/>
  ...
               </ns2:stackTrace>
            </ns2:exception>
         </detail>
      </S:Fault>
   </S:Body>
</S:Envelope>

The thing is that in my code as you see, it doesn't show the "detail" tag.问题是,如您所见,在我的代码中,它没有显示“详细信息”标签。 You guys know how to show this kind of information in the SOAP response?你们知道如何在 SOAP 响应中显示这种信息吗?

public String myMethod(String field1, String field2) throws Throwable_Exception {

        LOG.info("Executing operation myMethod");
        
        try {
            
            java.lang.String _return = "";
            
            List<String> testExceptions = new ArrayList<>();
            
            testExceptions.get(0);            
            return _return;
        
        } catch (IndexOutOfBoundsException ex) {
            throw new Throwable_Exception("TEST Custom Exception", ex.getCause());
        }
        
        //throw new Throwable_Exception("Throwable...");
    }

In the impl class I've tried to force an IndexOutOfBoundsException as well and tried to use this constructor:在 impl class 中,我也尝试强制使用 IndexOutOfBoundsException 并尝试使用此构造函数:

public Exception(String message, Throwable cause) {
        super(message, cause);
    }

and I thought that this approach could resolve the issue but apparently not.我认为这种方法可以解决问题,但显然不能。

Note: the WS is deployed on WildFly18.注:WS 部署在 WildFly18 上。

for everyone still looking for the answer I've understood that basically if you have an xsd schema, you have to define the complex type of your exception detail.对于仍在寻找答案的每个人,我了解到基本上如果您有 xsd 架构,则必须定义异常详细信息的复杂类型。 For example:例如:

  <xs:complexType name="YourCustomException">
    <xs:sequence>
      <xs:element name="exName" type="xs:string" minOccurs="0"></xs:element>
      <xs:element name="exMessage" type="xs:string" minOccurs="0"></xs:element>
      <xs:element name="exDetails" type="xs:string" minOccurs="0"></xs:element>
    </xs:sequence>
  </xs:complexType>

in Java your going to have the class from this complexType and his exception class. In the exception class you have to bring an attribute of the complexType (if you generate the classes with CXF or some other tools everything is taken care of) and you have to figure your preferred way for instanciate the complexType in your customException so in the SOAP Fault the "detail" tag is populated.在 Java 中,您将从这个 complexType 和他的异常 class 中获得 class。在异常 class 中,您必须带上 complexType 的属性(如果您使用 CXF 或其他一些工具生成类,一切都会得到处理)并且您有确定在 customException 中实例化 complexType 的首选方式,以便在 SOAP Fault 中填充“详细信息”标签。

Example of the CustomException in Java: Java 中的 CustomException 示例:

@WebFault(name = "YourCustomException", targetNamespace = "http://serviceendpoint/")
public class YourCustomException_Exception extends Exception {

    private it.your.package.YourCustomException YourCustomException;

    public YourCustomException_Exception() {
        super();
    }

    public YourCustomException_Exception(String message) {
        super(message);
    }

    public YourCustomException_Exception(String message, java.lang.Throwable cause) {
        super(message);
        this.YourCustomException = new YourCustomException();
        this.YourCustomException.setExName(cause.getClass().getName());
        this.YourCustomException.setExMessage(cause.getMessage());
        this.YourCustomException.setExDetails(cause.getLocalizedMessage());
        
    }
}

example of the ComplexType in Java: Java 中 ComplexType 的示例:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "YourCustomException ", propOrder = {
    "exName",
    "exMessage",
    "exDetails"
})
public class YourCustomException {

    protected String exName;  
    protected String exMessage;
    protected String exDetails;

    public String getExName() {
        return exName;
    }

    public void setExName(String exName) {
        this.exName = exName;
    }

    public String getExMessage() {
        return exMessage;
    }

    public void setExMessage(String exMessage) {
        this.exMessage = exMessage;
    }

    public String getExDetails() {
        return exDetails;
    }

    public void setExDetails(String exDetails) {
        this.exDetails = exDetails;
    }
    
    

}

SOAP Fault in the response forcing an IndexOutOfBounds in the method called: SOAP 在调用的方法中强制 IndexOutOfBounds 的响应错误:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Error in your method</faultstring>
         <detail>
            <ns2:YourCustomException xmlns:ns2="http://serviceendpoint/">
               <exName>java.lang.IndexOutOfBoundsException</exName>
               <exMessage>Index: 0, Size: 0</exMessage>
               <exDetails>Index: 0, Size: 0</exDetails>
            </ns2:YourCustomException >
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

Hope this helps.希望这可以帮助。

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

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