简体   繁体   中英

JAX-WS fault: throw superclass of exception

I want to throw subclasses of MyCustomException but for the superclass to be transferred across the web service; however, the subclass is transferred, instead. I have tried adding the annotation WebFault to the classes with no effect. I have provided an example of what is currently happening and an example of what I want to happen instead.

The exceptions.

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

The web service implementation. NB: I don't want to change what is being thrown here.

@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");
    }

}

The XML for throwCustomException() call using the web service.

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

The XML for throwCustomInnerException() call using the web service.

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

What I want to happen is the following , when throwCustomInnerException() is called using the web service:

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

You can change the method:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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