Java if 条件检查空值和 null 值

标签 java

如何在 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)

最佳答案

if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){

测试毫无意义getSpeedInfo()对于 null两次。这当然是显而易见的吗?这并没有测试 getBandwidth() 的情况单独为空。

根据您的要求:

  1. If <speedInfo> itself is not present
request.getSpeedInfo() == null
  1. <sch:SpeedInfo> is present, but bandwidth not present:
|| request.getSpeedInfo().getBandwidth() == null
  1. Bandwidth is present but no value
|| 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 ...

关于Java if 条件检查空值和 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142188/

相关文章:

java - 用 Paint 给位图着色?

java - Java 8 中嵌入式 JRE 中的 JDWP

java - 使用 JavaMail 阅读电子邮件内容时出现编码问题

java - 使用 JDBC 线程 "AWT-EventQueue-0"java.lang.NoClassDefFoundError 中出现异常

java - Scala.List 和 Java.util.List 之间的互操作

java - 由于 LogManager.getLogger(Logger.class.getName()); 导致 PermGen 空间增加;

java - 使用 DEFAULT id 的 INSERT 在 PostgreSQL 中不起作用

java - Hibernate - 访问新数据字段时无法解决属性错误

java - 比较器类显示错误排序

java - 我的错误是什么? java Greeter类文件和驱动程序文件。声明greeter类并在驱动程序文件中实现