簡體   English   中英

JAX-WS錯誤:拋出異常超類

[英]JAX-WS fault: throw superclass of exception

我想拋出MyCustomException子類,但要讓超類跨Web服務傳輸; 但是,子類將被轉移。 我嘗試將注釋WebFault添加到類中沒有任何效果。 我提供了一個當前正在發生的事的示例,以及一個我想發生的事的示例。

例外情況。

public class MyCustomException extends Exception {

    String text;

    public static class CustomInner extends MyCustomException {
        public CustomInner1() {
            super("inner");
        }
    }

    public MyCustomException(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

Web服務實現。 NB:我不想改變這里所扔的東西。

@Stateless(name = "MyService", mappedName = "MyService")
@LocalBean
@WebService(targetNamespace = "http://my.org/ns/")
public class MyService {

    @WebMethod
    public String throwCustomInnerException() throws MyCustomException {
        throw new MyCustomException.CustomInner();
    }

    @WebMethod
    public String throwCustomException() throws MyCustomException {
        throw new MyCustomException("text");
    }

}

使用Web服務的throwCustomException()的XML。

<?xml version='1.0' encoding='UTF-8'?>
<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>pack.MyCustomException</faultstring>
            <detail>
                <ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
                    <text>text</text>
                </ns2:MyCustomException>
            </detail>
        </S:Fault>
    </S:Body>
</S:Envelope>

使用Web服務的throwCustomInnerException()的XML。

<?xml version='1.0' encoding='UTF-8'?>
<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>pack.CustomInner</faultstring>
        </S:Fault>
    </S:Body>
</S:Envelope>

當使用Web服務調用throwCustomInnerException()時, 我想發生的事情如下

<?xml version='1.0' encoding='UTF-8'?>
<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>pack.MyCustomException</faultstring>
            <detail>
                <ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
                    <text>inner1</text>
                </ns2:MyCustomException>
            </detail>
        </S:Fault>
    </S:Body>
</S:Envelope>

您可以更改方法:

@WebMethod
public String throwCustomInnerException() throws MyCustomException {
    throw new MyCustomException.CustomInner();
}

至:

@WebMethod
public String throwCustomInnerException() throws MyCustomException {
    throw new MyCustomException(CustomInner.getClass().getSimpleName());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM