I’m developing a simple web service and client. See my snippet of service:
@webService
public Car carData(int id) {
try {
return carDAO.getCars(id);
} catch (NullPointerException e) {
System.out.println("Error: "+e.getLocalizedMessage());
}
return null;
}
Is is ok to returning null to client, and then the client should take care of null OR is it better that web service take care of null and throws exception?
UPDATE:
How can I return nullpointerexception instead of returning null?
What would you recommend?
Thanks
You can use SOAP faults in JAX-WS Web Services. This way, you can throw a custom exception when the prerequisites are not found. The client has a better idea of what happened.
For that, you need an Exception with
@WebFaultannotation. You can find a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services – Eben Hewitt on Java.In the Users Guide » GlassFish » Metro » JAX-WS you can find this example:
The Exception:
The JAX-WS runtime generate the Fault automatically.