返回null或抛出异常 [英] returning null or throw exception

查看:239
本文介绍了返回null或抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的Web服务和客户端。看到我的服务片段:

  @webService 
public Car carData(int id){
try {
return carDAO.getCars(id);
} catch(NullPointerException e){
System.out.println(Error:+ e.getLocalizedMessage());

}
返回null;

}



可以返回null给客户端,然后客户端应该关心null OR是否更好地Web服务处理null并抛出异常?



更新:
如何我可以返回nullpointerexception而不是返回null吗?



你会推荐什么?



谢谢

解决方案

您可以在JAX-WS Web服务中使用SOAP故障。这样,当您找不到先决条件时,您可以抛出自定义异常。客户端更好的了解发生了什么。



为此,您需要一个具有 @WebFault 注释的异常。您可以在在Java JAX-WS Web服务中使用SOAP故障和例外 - Eben Hewitt on Java



用户指南»GlassFish»Metro»JAX-WS 你可以找到这个例子:

  package fromjava.server; 

import javax.jws.WebService;

@WebService
public class AddNumbersImpl {
/ **
* @param number1
* @param number2
* @return总和
* @throws AddNumbersException如果要添加的任何数字是
* negative。
* /
public int addNumbers(int number1,int number2)throws
AddNumbersException {
if(number1< 0 || number2< 0){
throw新的AddNumbersException(负数不能+
添加!,Numbers:+ number1 +,+ number2);
}
return number1 + number2;
}
}

异常:

  package fromjavahandler.server; 

public class AddNumbersException extends Exception {

String detail;

public AddNumbersException(String message,String detail){
super(message);
this.detail = detail;
}

public String getDetail(){
return detail;
}
}

JAX-WS运行时自动生成故障。 / p>

I'm developing a simple web service and client. See my snippet of service:

 @webService
 public Car carData(int id)  {
   try {
    return carDAO.getCars(id);
    } catch (NullPointerException e) {
        System.out.println("Error: "+e.getLocalizedMessage());

    }
    return  null;

}

Is is ok to returning null to client, and then the client should take care of null OR is it better that web service take care of null and throws exception?

UPDATE: How can I return nullpointerexception instead of returning null?

What would you recommend?

Thanks

解决方案

You can use SOAP faults in JAX-WS Web Services. This way, you can throw a custom exception when the prerequisites are not found. The client has a better idea of what happened.

For that, you need an Exception with @WebFault annotation. You can find a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java.

In the Users Guide » GlassFish » Metro » JAX-WS you can find this example:

package fromjava.server;

import javax.jws.WebService;

@WebService
public class AddNumbersImpl {
    /**
     * @param number1
     * @param number2
     * @return The sum
     * @throws AddNumbersException if any of the numbers to be added is 
     * negative.
     */
    public int addNumbers(int number1, int number2) throws 
            AddNumbersException {
        if (number1 < 0 || number2 < 0) {
            throw new AddNumbersException("Negative number cant be " +
                    "added!", "Numbers: " + number1 + ", " + number2);
        }
        return number1 + number2;
    }
}

The Exception:

package fromjavahandler.server;

public class AddNumbersException extends Exception {

    String detail;

    public AddNumbersException(String message, String detail) {
        super(message);
        this.detail = detail;
    }

    public String getDetail() {
        return detail;
    }
}

The JAX-WS runtime generate the Fault automatically.

这篇关于返回null或抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆