Java if条件检查空值和空值

Java if条件检查空值和空值,java,Java,如何在java中为webservice请求编写条件,在我的webservice请求中,我传递的元素具有SpeedInfo。在SpeedInfo元素中,有两个子元素Bandwidth和AccessSpeed 这是我在SoapUI中的请求: <sch:SpeedInfo> <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:Bandwi

如何在java中为webservice请求编写条件,在我的webservice请求中,我传递的元素具有
SpeedInfo
。在
SpeedInfo
元素中,有两个子元素
Bandwidth
AccessSpeed

这是我在SoapUI中的请求:

 <sch:SpeedInfo>
    <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:Bandwidth>
    <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:AccessSpeed>
 </sch:SpeedInfo>
我需要检查3种情况:

 1. if <speedInfo> itself is not present

 2. <sch:SpeedInfo> is present, but bandwidth not present:

  <sch:SpeedInfo>
      <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
  </sch:SpeedInfo>

 3. Bandwidth is present but no value  
   <sch:SpeedInfo>
      <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1"></ns:Bandwidth>
      <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
   </sch:SpeedInfo>
null
测试
getSpeedInfo()
两次是没有意义的。这是显而易见的吗?这并没有测试单独的
getBandwidth()
为空的情况

根据您的要求:

  • 如果
    本身不存在
  • 存在,但带宽不存在:
  • 带宽存在,但没有价值
  • 总而言之:

    if (request.getSpeedInfo() == null
    ||
    request.getSpeedInfo().getBandwidth() == null
    ||
    request.getSpeedInfo().getBandwidth().isEmpty())
    {
        // request is invalid
    }
    else
        // request is valid ...
    
    null
    测试
    getSpeedInfo()
    两次是没有意义的。这是显而易见的吗?这并没有测试单独的
    getBandwidth()
    为空的情况

    根据您的要求:

  • 如果
    本身不存在
  • 存在,但带宽不存在:
  • 带宽存在,但没有价值
  • 总而言之:

    if (request.getSpeedInfo() == null
    ||
    request.getSpeedInfo().getBandwidth() == null
    ||
    request.getSpeedInfo().getBandwidth().isEmpty())
    {
        // request is invalid
    }
    else
        // request is valid ...
    

    .equals(“”
    由于没有这样的方法,请不要将其“固定”到
    .equals(“”
    ),而是使用
    .isEmpty()
    .equals(“”)和
    .length()==0
    看起来总是有点奇怪。@Tom谢谢。。getBandwidth()是一个整数,使用isEmpty()它也不起作用。length()==0不起作用。请解释“带宽存在但没有值”的意思,这不只是给您一个空对象吗?我的意思是,如果类型是
    java.lang.Integer
    ,你怎么能有一个“空”整数呢?
    .equals(“”
    ),既然没有这样的方法,请不要将它“修正”为
    .equals(“”
    ),而是使用
    .isEmpty()
    .equals(“”)和
    .length()==0
    看起来总是有点奇怪。@Tom谢谢。。getBandwidth()是一个整数,使用isEmpty()它也不起作用。length()==0不起作用。请解释“带宽存在但没有值”的意思,这不只是给您一个空对象吗?我的意思是,如果类型是
    java.lang.Integer
    ,你怎么能有一个“空”整数呢?
    request.getSpeedInfo()==null&&request.getSpeedInfo().getBandwidth()==null
    没有多大意义。这就像问
    如果(foo==null&&foo.bar()==null)
    。现在,如果
    foo
    将为null,那么您的第二个条件将被执行,这意味着
    foo.bar()
    将类似于
    null.bar()
    ,但是由于
    null
    没有任何成员,您将获得NPE。
    request.getSpeedInfo()==null&&request.getSpeedInfo().getBandwidth()==null
    没有多大意义。这就像问
    如果(foo==null&&foo.bar()==null)
    。现在,如果
    foo
    将为null,那么您的第二个条件将被执行,这意味着
    foo.bar()
    将类似于
    null.bar()
    ,但是由于
    null
    没有任何成员,您将获得NPE。因此,使用类必须测试的任何API来测试空性。由于您没有告诉我们带宽属于哪一类,我猜测它是一个
    字符串。
    它也适用于
    集合
    。它是一个整数类型
    public Integer getBandwidth(){return bandwidth;}
    ,因为它是一个整数,不能为空,如果它为空,xsd会自动抛出一个错误。第三个条件是不需要的。如果它是一个整数,那么你的问题中关于“无值”的部分就没有意义了。所以使用类必须测试的任何API来检查空值。由于您没有告诉我们带宽属于哪一类,我猜测它是一个
    字符串。
    它也适用于
    集合
    。它是一个整数类型
    public Integer getBandwidth(){return bandwidth;}
    ,因为它是一个整数,不能为空,如果它为空,xsd会自动抛出一个错误。第三个条件是不需要的。如果它是一个整数,你问题中关于“无值”的部分就没有意义了。
    SpeedInfo speedInfo = request.getSpeedInfo();
    if (speedInfo == null ||                // short circuit here means
        speedInfo.getBandwidth() == null || // that here speedInfo is not null
        speedInfo.getBandwidth().isEmpty()) { // Assuming getBandwidth() is a String or similar
    
            throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
    }
    
    if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){
    
    request.getSpeedInfo() == null
    
    || request.getSpeedInfo().getBandwidth() == null
    
    || request.getSpeedInfo().getBandwidth().isEmpty() // or whatever API call is appropriate to the datatype
    
    if (request.getSpeedInfo() == null
    ||
    request.getSpeedInfo().getBandwidth() == null
    ||
    request.getSpeedInfo().getBandwidth().isEmpty())
    {
        // request is invalid
    }
    else
        // request is valid ...