0

I want define service as web service. I define a method with register name. Its a class as input parameter with RegisterParam name and a return type with RegisterResult name as output.

public class RegisterParam{
      String name;
      String family;
}

public class RegisterResult{
      Integer registr_id;
}

public interface Service{
      public RegisterResult register(RegistrParam param);
}

If register service failed and its logic not doing so I have two solution for notify to service caller:

  1. In RegisterResult class add a property as result_code and a enumeration for its value. If its value is zero means register sercice successfully done and if its value is else of zero means register service failed and result_code shows reason of fail.

  2. register service throws a exception when it is falied and if dont any exception throwed means register service done successfully.

My question is: what above solution is bettr and why?

Sam
  • 6,770
  • 7
  • 50
  • 91

2 Answers2

0

I think you should differ two different scenarios:

1) An exception occurs, like some environment (e.g. database) is not available. In this case I would throw an exception which is automatically converted into an SOAPFault by the web service implementation. The advantage on this one is, that the caller of your service get's also an exception and can handle that by it's exception handling.

2) An expected business "error" happend, like the user is not logged in or did not provide the necessary information to enable the service to complete successful. The caller of your web service receives an response message which the caller has to interpret and maybe wants to show it to the user which provided the information.

At the end it depends on your interface contract you provide to your interface users! Most important on that one is to describe how your service handles different errors scenarios.

0

case 1 no result:

public interface Service{
    public void register(RegistrParam param);
}

In this case your Service can send a HTTP Status Code like 200. Your client will interpret the status code this way:

  • 200: ok i'm registered
  • 400: hm don't know why but failed...

If you want to understand what happened and why is your request failed with 400 error, you have to check your server logs or something like that.

case 2 with result:

public interface Service{
    public Response register(RegistrParam param);
}

I'm sending always a Response object to my clients:

public class Response {

    private ResponseStatus status;     // enum: (OK, ERROR, BAD_REQUEST)
    private ResultType     type;       // enum: (RegisterResult, LoginResult etc...) defines how to interpret the field "data"
    private Object         data;       // RegisterResult.java as jsonstring or Exception.java as jsonstring etc...
    private long           time = -1L; // execution time

}

If I'm developing my client, I can always understand why my request was failed.

alex
  • 8,904
  • 6
  • 49
  • 75