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