简体   繁体   中英

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. Instead, I'm getting back an instance with all properties set to null. I can check for the item having a null property, but would prefer the item returned itself to be 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

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

Checking the item property will work on the client side

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? 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.

EDIT 2:
I initially approached this as a pure Java problem, but based on your latest comment, that's not true. It was pointed out in the chat that this is probably has to do with your annotations, or some other kind of broader settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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