Java 如何使用 JAX-WS webfault

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6606512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:09:02  来源:igfitidea点击:

how to use JAX-WS webfault

javaweb-servicesjax-ws

提问by Arvind

I have written a webservice and tried to throw my custom exception but i am getting error please help me to solve it.

我编写了一个网络服务并尝试抛出我的自定义异常,但出现错误,请帮助我解决它。

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

ERROR MESSAGEException in thread "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) at 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) at 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) at javax.xml.ws.Endpoint.publish(Endpoint.java:170) at mytest.com.inc.WebServiceTest.main(WebServiceTest.java:13)

错误信息线程“main”中的异常 java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String; 在 com.sun.xml.ws.model.RuntimeModeler.processExceptions(RuntimeModeler.java:1162) 在 com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:898) 在 com.sun.xml.ws .model.RuntimeModeler.processMethod(RuntimeModeler.java:666) 在 com.sun.xml.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:420) 在 com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler) .java:254) 在 com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:338) 在 com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:201) 在 com。 sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505) 在 com.sun.xml.ws.transport.http.server.EndpointImpl。

采纳答案by CodeClimber

Old question I know but I think the method names in your FaultBean class need to be getMessage and setMessage (as opposed to getMsg and setMsg) - see this excellent article:

我知道的老问题,但我认为您的 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

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

回答by gastush

Last time I saw this error, it was due by a missmatch of version of JAX-WS used (2.1.x and 2.2.x) You are maybe compiling against the 2.1.x version but running against 2.2.x, or the other way round.

上次我看到此错误时,是由于使用的 JAX-WS 版本不匹配(2.1.x 和 2.2.x)您可能正在针对 2.1.x 版本进行编译但针对 2.2.x 运行,或者其他方式圆形的。

Keep in mind that the JDK 6 includes the 2.1.x version of JAX-WS, so you might have to use the endorsed library mechanism to force the usage of 2.2.x

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