简体   繁体   English

从Axis Web服务手动抛出什么样的异常?

[英]What kind of exception to manually throw from an Axis web service?

I have some webservices that are developed on axis 1.1, and I need to make a few changes. 我有一些在1.1轴上开发的web服务,我需要进行一些更改。 During this I am fixing up the exception handling code, but I don't know what kind of exception I should throw when there is an error. 在此期间,我正在修复异常处理代码,但我不知道在出现错误时应该抛出什么样的异常。

I only need to send the message to the client, I don't need to worry about stack traces and custom information in the exceptions. 我只需要将消息发送给客户端,我不需要担心异常中的堆栈跟踪和自定义信息。 I don't want to deal with extending soapfaults, or providing reasons for the failures, and all that jazz. 我不想处理扩展的soapfaults,或提供失败原因,以及所有爵士乐。

@WebMethod
public string[] myMethod() throws ..?.. {
    throw new AxisFault(); // not recommended
    throw new SOAPFaultException();  // seems overly general
    throw new Exception(); // what we have now
}

Is there any right way to do this, or is throw new Exception the right way to go about it? 有没有正确的方法来做到这一点,或者throw new Exception是正确的方法呢?

You may create a custom exception (say FooException ) extending Exception annotated with JAX-WS @WebFault . 您可以创建一个自定义异常(比如FooException ),扩展使用JAX-WS @WebFault注释的Exception

@WebFault(faultBean = "org.foo.bar.FooFault")
public class FooException extends Exception {
    private FooFault fooFault;

    public FooException() {
        super();
    }

    public FooException(String message, FooFault fooFault, Throwable cause) {
        super(message, cause);
        this.fooFault = fooFault;
    }

    public FooException(String message, FooFault fooFault) {
        super(message);
        this.fooFault = fooFault;
    }

    public FooFault getFaultInfo() {
        return fooFault;
    }
}

// this is org.foo.bar.FooFault
public class FooFault {
    // POJO
}

And then you declare that your web method throws that exception. 然后你声明你的web方法抛出了那个异常。

@WebMethod
public string[] myMethod() throws FooException {
    // do some stuff
    throw new FooException();
    // or with a cause
    try { 
        // something dangerous
    } catch (Exception e) {
        throw new FooException("Shit happens", new FooFault(), e);
    }
    // or like this
    throw new FooException("Foo", new FooFault());
}

JAX-WS should do the rest. JAX-WS应该完成其余的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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