简体   繁体   English

让Java Web服务返回Null Web结果

[英]have a java web service return a Null Web Result

I have a Java web service that when no item is found I want a null object to be returned. 我有一个Java Web服务,当找不到任何项目时,我希望返回一个空对象。 Instead, I'm getting back an instance with all properties set to null. 取而代之的是,我找回一个实例,将所有属性设置为null。 I can check for the item having a null property, but would prefer the item returned itself to be Null. 我可以检查具有null属性的项目,但希望返回自身的项目为Null。

@WebMethod
@WebResult(name="item")
public item findItem(
            @WebParam(name="Id") int Id){
  //...
  if(ItemNotFound(Id)){
    return null;
  }
}

On the client side, the wsItem is NOT null 在客户端,wsItem不为null

item wsItem=ws.findItem(1);
if(wsItem==null){
  // this will not be hit
}

Checking the item property will work on the client side 检查item属性将在客户端运行

item wsItem=ws.findItem(1);
    if(wsItem.Property1==null){
      // this will
    }

Based on the information you've given, I don't understand why you can't simply do 根据您提供的信息,我不明白为什么您不能简单地做

if(item.getProperties() == null)
    return null;

EDIT: 编辑:
Okay, now that you've updated with more code, I have a better idea of what's going on. 好的,既然您已经更新了更多代码,那么我对发生的事情有了更好的了解。 Can you set some breakpoints? 您可以设置一些断点吗? Are you sure that ItemNotFound is returning true , and that you're actually reaching the return null line in your service code? 您确定ItemNotFound返回的是true ,并且您实际上是在服务代码中到达return null行吗? Something extra-kooky would have to be going on for the client code to be responsible, since I don't see the new keyword anywhere in there. 因为我看不到其中的new关键字,所以必须执行一些额外的操作才能使客户端代码负责。

EDIT 2: 编辑2:
I initially approached this as a pure Java problem, but based on your latest comment, that's not true. 我最初将其作为纯Java问题解决,但是基于您的最新评论,事实并非如此。 It was pointed out in the chat that this is probably has to do with your annotations, or some other kind of broader settings. 聊天中指出,这可能与您的注释或其他某种更广泛的设置有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM