daramasala
daramasala

Reputation: 3030

Best practice for java web services and remote exception

I am writing a web service. So I have:

public interface MyService extends Remote
{
  public void service1();
}

public class MyServiceImpl implements MyService
{
  public void service1() { /* do something that sometimes throws an exception */ }
}

I was reading about RemoteException. Should I wrap all the code in service1() in a try..catch that wraps any exception in a RemoteException? Then I will have to do it for service2(), service3() and so on.

Is it better to let the invoker (a servlet in my case) to do this?

What exactly should be wrapped in remote exception - everything that happens in the server? or only exceptions dealing with the remote invocation process (reflection, serialization, etc)?

Upvotes: 0

Views: 2731

Answers (2)

tw39124
tw39124

Reputation: 9203

As far as I know you only need to declare that the methods throw RemoteException in the interface.

public interface MyService extends Remote {
  public void service1() throws RemoteException;
}

Upvotes: 0

Nick Holt
Nick Holt

Reputation: 34301

Your code shouldn't be throwing RemoteException, it should be reserved for the WS framework if and when errors occur accessing the remote service.

Most of the frameworks will catch Exception, wrap it up as a SOAP-Fault, which is the client deals with normally by throwing another exception with the error message from the SOAP-Fault (remember the client may not be Java).

The up-shot of this is you should throw exceptions specific to the fault that has occurred, leaving the framework to send the appropriate response.

Upvotes: 2

Related Questions