【问题标题】:Java if condition check for empty and null valuesJava if 条件检查空值和空值
【发布时间】:2016-02-01 23:09:11
【问题描述】:

如何在 java 中为 web 服务请求编写条件,在我的 web 服务请求中,我传递了一个具有 SpeedInfo 的元素。在SpeedInfo 元素中有两个子元素BandwidthAccessSpeed

这是我在 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>

我的 Java 条件:

if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){
    throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
}

我需要我的条件来检查 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>

我得到的错误:

2016-02-02 09:45:48,349  WARN http-bio-8080-exec-3--[c0a8381152a2a943f51] c0a8381152a2a943f51 fw.ws.MessageInInterceptor:49 - Content input stream is NOT null, nothing to log in handleFault()?
2016-02-02 09:45:48,351  WARN http-bio-8080-exec-3--[c0a8381152a2a943f51] c0a8381152a2a943f51 cxf.phase.PhaseInterceptorChain:452 - Interceptor for {http://lpp.att.com/logical/v1}LogicalService#{http://lpp.att.com/logical/v1}classOfServiceAllocation has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Unmarshalling Error: cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'. 
at    org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:908)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:712)

【问题讨论】:

  • request.getSpeedInfo() == null &amp;&amp; request.getSpeedInfo().getBandwidth() == null 没有多大意义。这就像问if (foo == null &amp;&amp; foo.bar() == null)。现在如果foo 为空,那么您的第二个条件将被执行,这意味着foo.bar() 将类似于null.bar(),但由于null 没有任何成员,您将获得NPE。

标签: java


【解决方案1】:
if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){

getSpeedInfo() 测试两次null 是没有意义的。这当然很明显?这并不能测试 getBandwidth() 单独为空的情况。

根据您的要求:

  1. 如果&lt;speedInfo&gt; 本身不存在
request.getSpeedInfo() == null
  1. &lt;sch:SpeedInfo&gt; 存在,但带宽不存在:
|| request.getSpeedInfo().getBandwidth() == null
  1. 存在带宽但没有值
|| 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 ...

【讨论】:

  • 所以使用类必须测试空的任何 API。由于您没有告诉我们带宽属于哪个等级,我猜测它是 String. 它也适用于 Collection
  • 它是一个整数类型public Integer getBandwidth() { return bandwidth; }
  • 因为它是一个整数,它不能为空,如果它是空的,xsd 会自动抛出一个错误。第三个条件是不需要的。
  • 如果它是一个整数,那么你关于“没有价值”的部分是没有意义的。
【解决方案2】:
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 ");
}

【讨论】:

  • .equas("") 因为没有这样的方法,请不要将其“修复”为.equals(""),而是使用.isEmpty().equals("").length() == 0 总是看起来有点怪。
  • ..getBandwidth() 是一个整数,使用 isEmpty() 也不起作用 .length() == 0 也不起作用
  • 那么请解释一下“带宽存在但没有价值”是什么意思
  • 这不只是给你一个空对象吗?我的意思是,如果类型是java.lang.Integer,你怎么会有一个“空”的整数?