Best practice for java web services and remote exception

点点圈 提交于 2020-01-06 06:47:08

问题


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


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/1322222/best-practice-for-java-web-services-and-remote-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!