0

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

Mitja Rogl
  • 894
  • 9
  • 22
  • 39
  • 3
    If it's web, why not return a valid HTTP response code with the corresponding error message? – David Jan 08 '13 at 22:10
  • For now I'm just testing in console :) – Mitja Rogl Jan 08 '13 at 22:12
  • The best way would be to give a valid answer. If there's any kind of exception, it would be better to throw your own service exception (fault) and let the client to handle it. More info: [SOAP faults or results object?](http://stackoverflow.com/q/2823100/1065197) – Luiggi Mendoza Jan 08 '13 at 22:13
  • 1
    Is this the UI layer or the logic layer? If it's the UI layer and the UI is HTTP-based then the proper response is an HTTP response with a status code and some form of content (error message, for example). However you're testing it should include an HTTP harness of some kind of replicate the application context. Unit testing the logic layer can skip the HTTP harness because there's no UI involved. – David Jan 08 '13 at 22:14
  • Just forget about web service and client. I just want to know which approach is better to use in basic examples. I just updated the question. – Mitja Rogl Jan 08 '13 at 22:18

3 Answers3

1

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 @WebFault annotation. 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:

package fromjava.server;

import javax.jws.WebService;

@WebService
public class AddNumbersImpl {
    /**
     * @param number1
     * @param number2
     * @return The sum
     * @throws AddNumbersException if any of the numbers to be added is 
     * negative.
     */
    public int addNumbers(int number1, int number2) throws 
            AddNumbersException {
        if (number1 < 0 || number2 < 0) {
            throw new AddNumbersException("Negative number cant be " +
                    "added!", "Numbers: " + number1 + ", " + number2);
        }
        return number1 + number2;
    }
}

The Exception:

package fromjavahandler.server;

public class AddNumbersException extends Exception {

    String detail;

    public AddNumbersException(String message, String detail) {
        super(message);
        this.detail = detail;
    }

    public String getDetail() {
        return detail;
    }
}

The JAX-WS runtime generate the Fault automatically.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

There's probably going to be a million different opinions on this, but I'll tell you what I'd do.

Typically remote services are already capable of throwing IOException when remote calls are made and subsequently fail, so I like to try to spare my clients the possibility of ever having to deal with RuntimeException or any of its subclasses (which includes NullPointerException). If your service throws a RuntimeException and the client prints out the stack trace, it's confusing to the client-side developer because the stack trace pertains to code running on the server that the developer may have little or no control over. Ideally, the client should only have to handle cases where null was returned instead of the expected response. Only throw an Exception on the server side if null is not sufficient to tell the client that something went wrong or if the problem stems from an issue with the inputs supplied by the client.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36
0

You shouldn't be getting an NPE. if you do, it probably means your service isn't initialized and you should probably throw an IllegalStateException or reply with a message back to the client to say that the web service is unavailable

Bohemian
  • 412,405
  • 93
  • 575
  • 722