如何使用JAX-WS webfault

时间:2011-07-07 06:09:47

标签: java web-services jax-ws

我写了一个webservice并试图抛出我的自定义异常,但是我收到错误请帮我解决。

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService(name = "WebService")
public class WebServiceTest {
    public String sayHello(String name) throws InvalidInputException {
        throw new InvalidInputException("I am testing", null);
    }
    public static void main(String[] args) {
        WebServiceTest server = new WebServiceTest();
        Endpoint endpoint = Endpoint.publish(
                "http://localhost:9191/webServiceTest", server);
    }
}


import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "InputMessageValidationFaultType")
public class FaultBean {

    @XmlAttribute
    protected String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String value) {
        this.msg = value;
    }

}


import javax.xml.ws.WebFault;

@WebFault(faultBean = "mytest.com.inc.FaultBean")
public class InvalidInputException extends Exception {

    private static final long serialVersionUID = 1L;

    private FaultBean faultBean;

    public InvalidInputException() {
        super();
    }

    public InvalidInputException(String message, FaultBean faultBean,
            Throwable cause) {
        super(message, cause);
        this.faultBean = faultBean;
    }

    public InvalidInputException(String message, FaultBean faultBean) {
        super(message);
        this.faultBean = faultBean;
    }

    public FaultBean getFaultInfo() {
        return faultBean;
    }
}

错误消息 线程“main”中的异常java.lang.NoSuchMethodError:javax.xml.ws.WebFault.messageName()Ljava / lang / String;     at com.sun.xml.ws.model.RuntimeModeler.processExceptions(RuntimeModeler.java:1162)     at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:898)     at com.sun.xml.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:666)     在com.sun.xml.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:420)     at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:254)     at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:338)     at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:201)     在com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505)     at com.sun.xml.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:257)     at com.sun.xml.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:181)     at com.sun.xml.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:123)     在javax.xml.ws.Endpoint.publish(Endpoint.java:170)     在mytest.com.inc.WebServiceTest.main(WebServiceTest.java:13)

2 个答案:

答案 0 :(得分:12)

上次我看到这个错误,是由于使用的JAX-WS版本不匹配(2.1.x和2.2.x) 您可能正在编译2.1.x版本,但是针对2.2.x运行,反之亦然。

请记住,JDK 6包含2.1.x版本的JAX-WS,因此您可能必须使用支持的库机制来强制使用2.2.x

答案 1 :(得分:7)

旧问题我知道,但我认为FaultBean类中的方法名称需要是getMessage和setMessage(而不是getMsg和setMsg) - 请参阅这篇优秀的文章:

http://io.typepad.com/eben_hewitt_on_java/2009/07/using-soap-faults-and-exceptions-in-java-jaxws-web-services.html