java - 如何从 web 服务中抛出异常?

标签 java web-services exception throw

我正在使用netbeans制作Web服务,我想使用PBEL制作复合Web服务, 我在每个服务中抛出异常时遇到问题,我在要抛出的异常的架构中定义了复杂类型,并且我也在 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 block 中。

throw new IsCreditCardValidFault();

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

相关文章:

c# - WSDoAllReceiver : Incoming message does not contain required Security header

python - 为什么 mp.Pool fast 会失败,而 ProcessPoolExecutor 不会?

python - 如何使用循环直到将键添加到字典中?

java - 解决Spring与@Autowired和@Qualifier的冲突

java - 如何查明我正在使用 Perl 的 Java 版本?

java - 为什么这不起作用(java中的ArrayList)?

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

java - 创建 Java Applet 指南,无提示打印

java - 如何通过restful web服务返回集合对象

java - REST Web 服务 : synchronous or asynchrous?