Friday, July 17, 2009

JAX-WS and Operation Overloading

Another great group of people this week in my classroom. The topic was Java Web Services with a little JMS thrown in. During the class this week, in the discussion of Java to XML (and back) mapping as provided by JAX-WS and JAXB, I indicated to students that while Java as a programming language supports method (or operation) overloading, it is prohitited in Web services by specification (WSDL) and the WS-I Basic Profile.

During class, we explored the JAX-WS annotations for defining Java Web services. Specifically, we looked at the @WebService annotation on the service endpoint interface (SEI) and implementation bean (SIB) as shown below.
package example;
import
javax.jws.WebService;
@WebService(endpointInterface ="example.SomeService")
public class SomeServiceImpl implements SomeService
{
public int doIt(String str) {
return 0;
}
}

package example;
import
javax.jws.WebService;
@WebService
public interface SomeService {
public
int doIt(String str);
}
When looking at JAX-WS, we also explored the operationName attribute on the @WebMethod annotation for Web service methods. A perceptive and imaginative student (thanks Pat) asked “if you could use the operationName to allow for method overloading at the Java level while preserving unique operation names in the WSDL and SOAP level?” I’ve extended the example to demonstrate below.

package example;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface SomeService {

public int doIt(String str);
@WebMethod(operationName = "doItNow")
public int doIt(int i);
}

package example;
import javax.jws.WebService;
@WebService(endpointInterface = "example.SomeService")
public class SomeServiceImpl implements SomeService {
public int doIt(String str) {
return 0;
}
public int doIt(int i) {
return 0;
}
}
What a great question and one that sent me scrambling to my browser and text books looking for an answer.

The answer is – no, not easily. This will be somewhat dependent on the JAX-WS implementation (for example, take a look at Christophe Hamerling’s example in Apache CXF where overloaded operations and use of the @operationName works) and work-arounds could probably be negotiated (for example, don’t use document/literal wrapped style). However, when using the JAX-WS reference implementation mapping tools there is still going to be problems despite the @WebMethods operationName attribute provides a unique operation name as far as SOAP and WSDL are concerned.

The reason? The wsgen tool has problems creating the default SOAP wrapper classes for the operation inputs and outputs. When you run wsgen with the code above, you get an exception (com.sun.tools.internal.ws.processor.modeler.ModelerException: [failed to localize] Request wrapper bean names must be unique and must not clash with other generated classes.)as shown below.


So thanks again for the great question Pat. Contact Intertech if you would like to sign up for our next Complete Java Web Services class.

3 comments:

  1. I love it this post. It is vey enlightening!

    ReplyDelete
  2. BTW, why do you guys make it so hard to comment!?

    ReplyDelete
  3. my best friend Sildenafil Citrate was lecturing me on Java Web Services, but I was not understanding a thing and I had to stop him from keep doing that

    ReplyDelete