我正在使用Netbeans制作Web服务,我想使用PBEL制作复合Web服务,
我在每个服务中都引发异常时遇到了一个问题,我在要抛出的异常的模式中定义了复杂的Type,我也在WSDL中创建了它,但是在服务内部,我不知道如何抛出异常,这是我正在处理的示例:

@WebService(serviceName = "CreditCardService", portName = "CreditCardPort", endpointInterface = "org.netbeans.j2ee.wsdl.creditcard.CreditCardPortType", targetNamespace = "http://j2ee.netbeans.org/wsdl/CreditCard", wsdlLocation = "WEB-INF/wsdl/NewWebServiceFromWSDL/CreditCard.wsdl")
public class NewWebServiceFromWSDL implements CreditCardPortType {

public org.netbeans.xml.schema.creditcard.CreditCardResponseType isCreditCardValid(org.netbeans.xml.schema.creditcard.CreditCardType creditCardInfoReq) throws IsCreditCardValidFault {

    List<CreditCardType> creditCards = parseCreditCardsFile();
    CreditCardResponseType creditCardResponseElement = new CreditCardResponseType();

    for (CreditCardType aCreditCard : creditCards) {

        if (creditCardInfoReq.getCreditCardNo() == Long.parseLong(String.valueOf(aCreditCard.getCreditCardNo())) {
            creditCardResponseElement.setValid(true);
            return creditCardResponseElement;
        }
    }
    throws  IsCreditCardValidFault();   //here I want to throw an exception .
}


请有人帮忙吗?

最佳答案

throws  IsCreditCardValidFault();   //here I want to throw an exception .


需要写成

throw new IsCreditCardValidFault();


在方法声明中使用throws,在方法内部使用throw关键字指示将在何处引发异常。

举个例子

try {
   //do something which generates an exception
}catch(Exception e){
   throw e;
}


但是在您的情况下,您想自己启动异常,因此必须创建该异常类型的新对象。您将自己创建异常,因此无需将其包含在try / catch块中。

throw new IsCreditCardValidFault();

关于java - 如何从Web服务引发异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4563742/

10-12 00:37
查看更多