【问题标题】:JAX-WS fault: throw superclass of exceptionJAX-WS 错误:抛出异常的超类
【发布时间】:2016-06-01 08:31:22
【问题描述】:

我想抛出 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 服务实现。 注意:我不想更改这里抛出的内容。

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

当使用网络服务调用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>

【问题讨论】:

    标签: java web-services jakarta-ee jax-ws


    【解决方案1】:

    你可以改变方法:

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

    到:

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

    【讨论】:

    • 我实际上并不想更改抛出的内容(因为这是实际代码的简化版本)。
    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多