Can we implement method overloading in web service class?

别说谁变了你拦得住时间么 提交于 2019-11-26 21:37:59

问题


I would like to implement method overloading in the Java web service class as follows:

public String myMethod(User user)
{
    // My code
} 

public String myMethod(User[] user)
{
    for(int i=0; i<user.length; i++)
    {
        myMethod(user[i]);
    }
}

If I forward a single User object to myMethod(), it should trigger the first method and if I send an array of Users, it should trigger the second method.

In the WSDL file it shows only a single method. However, if I try to call @WebMethod(operationName="") for both calls, I am unable to generate the WSDL file.


回答1:


Operation overloading is not allowed for web services.
It is explicitely prohibited in WS-BP and WSDL 1.2 also disallows it.
Even if you found a stack that has some support for this I would recommend not to follow this approach.
Overloading is an OO concept. Don't try to apply them to Service Oriented paradigm




回答2:


Overloading the web service methods is not difficult. With Axis 1.4 at least it is fairly simple. If there are two overloaded methods in the service like below:

public String myMethod(String firstName, String lastName) throws RemoteException
public String myMethod(String name) throws RemoteException

Then a request like this:

http://localhost:8080/services/testService?method=myMethod&name=<name> 

will invoke the second method.

And a request like this one:

http://localhost:8080//services/testService?method=myMethod&firstName=<first_name>&lastName=<last_name>

will invoke the first method.

The resolution is done by Axis.



来源:https://stackoverflow.com/questions/10320006/can-we-implement-method-overloading-in-web-service-class

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