Web Services HTTP< detail>响应中的SOAP错误不可见

rslzwgfq  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(177)

我有一个问题。我已经从wsdl生成了一个WS服务器,我正在尝试理解异常处理。基本上在WSDL中定义了一个名为Throwable_Exception的自定义异常。现在我已经尝试测试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>

“faultstring”标签中的文本是我在异常中抛出的消息。现在我的目标是在响应中发送以下信息:
1.有关异常的可读消息
1.从服务器抛出的异常
1.最后,有关异常的详细信息
我在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>

问题是,在我的代码中,正如你们所看到的,它没有显示“detail”标记。你们知道如何在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...");
    }

在impl类中,我也尝试强制IndexOutOfBoundsException,并尝试使用以下构造函数:

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

我认为这种方法可以解决问题,但显然不是。
注:WS部署在WildFly18上。

sr4lhrrt

sr4lhrrt1#

对于每个仍在寻找答案的人,我已经了解到,基本上,如果您有一个xsd架构,则必须定义异常详细信息的复杂类型。例如:

<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>

在Java中,你将拥有这个complexType的类和它的exception类。在exception类中,你必须引入complexType的一个属性(如果你用CXF或其他工具生成类,一切都会处理好),并且你必须在customException中找到你首选的示例化complexType的方法,这样在SOAP Fault中,“detail”标记就会被填充。
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());
        
    }
}

Java中复杂类型的示例:

@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错误强制在调用的方法中执行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>

希望这对你有帮助。

相关问题