Can we implement method overloading in web service class?
Asked Answered
L

2

7

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.

Lorca answered 25/4, 2012 at 16:37 Comment(1)
I don't think it's possible to overload a webservice, and even if it is, it's certainly not good practiceDesiccator
K
16

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

Killer answered 25/4, 2012 at 16:48 Comment(1)
Overloading isn't an OO concept and it doesn't have any relation with OO, for example, the C language is a procedural/structured language and it offers functions overloading.Churl
E
5

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.

Ezraezri answered 25/4, 2012 at 16:48 Comment(1)
+1, good info. Though I worked with Axis for a good while, I didn't know Axis could do that. I would never go about it this way since it is not "kosher" web service design, but it is good to know if I ever run into a corner case where I must implement such a thing.Honesty

© 2022 - 2024 — McMap. All rights reserved.