Publishing a web service with out creating an Interface
java, jax-ws, web-services, wsdl
Solution
Is creating an service interface absolutely essential in creating a web service?
No, it's not necessary.
What I wanted to know is, can I combine both the interface and the implemntor into one
Yes,you can. Just remove endpointInterface declaration in Webservice annotation at your implementation class.
@WebService(endpointInterface="com.ad.ws.PowerCalculatorImpl")
So your implementation class will be,
package com.ad.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
@WebService()
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED)
public class PowerCalculatorImpl
{
@WebMethod public Double raisedToThePower(Double base, Double power)
{
return Math.pow(base, power);
}
}
Above code should work perfectly fine without any error.
I have tried following scenario from my end and it is working perfectly fine.
My implementation class,
package com.KingAm.HelloWorld;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
@WebService
@SOAPBinding(style = Style.RPC, use=Use.LITERAL, parameterStyle= ParameterStyle.WRAPPED)
public class helloWorldImpl{
@WebMethod(action="hello",operationName="helloWorld")
@WebResult(name="response1")
public String tMethod(String a, String b, String c)
{
if(!c.equals(null))
return "hello "+a+b+c;
else
return "okok";
}
//@Override
@WebMethod(action="hello2",operationName="helloWorld2")
@WebResult(name="response2")
public String tMethod2(String a, String b) {
// TODO Auto-generated method stub
return null;
}
}
and My publisher class is,
package com.KingAm.HelloWorld;
import javax.xml.ws.Endpoint;
//import helloWorldImpl;
public class helloWorldPublisher {
/**
* @param args
*/
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/ws/helloWorld", new helloWorldImpl());
System.out.println("Hello World Server is published!");
}
}
Please check your code once, you must be missing something.
Problem
I am using JAX-WS to create a web service and publish it. What I wanted to know is; Is it possible to publish an webservice with out creating an Interface. Meaning, right now I create an interface End point interface class: ``` package com.ad.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.ParameterStyle; import javax.jws.soap.SOAPBinding.Style; import javax.jws.soap.SOAPBinding.Use; @WebService @SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED) public interface PowerCalculator { @WebMethod Double raisedToThePower(Double base, Double power); } ``` Then I create a interface implementor class: ``` package com.ad.ws; import javax.jws.WebService; @WebService(endpointInterface="com.ad.ws.PowerCalculator") public class PowerCalculatorImpl implements PowerCalculator { @Override public Double raisedToThePower(Double base, Double power) { return Math.pow(base, power); } } ``` After this I publish using something like this: ``` package com.ad.endpoint; import javax.xml.ws.Endpoint; import com.ad.ws.PowerCalculatorImpl; public class PowerCalculatorPublisher { public static void main(String[] args) { System.out.println("Starting publish of the PowerCalculator service..."); Endpoint.publish("http://myhostname.ad.com:8585/ayusman/PowCalc", new PowerCalculatorImpl()); } } ``` What I wanted to know is, can I combine both the interface and the implemntor into one; something like: ``` package com.ad.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.ParameterStyle; import javax.jws.soap.SOAPBinding.Style; import javax.jws.soap.SOAPBinding.Use; @WebService(endpointInterface="com.ad.ws.PowerCalculatorImpl") @SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED) public class PowerCalculatorImpl { @WebMethod public Double raisedToThePower(Double base, Double power) { return Math.pow(base, power); } } ``` When I do this and start the publisher I get the following stack trace ``` Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: The web service defined by the class com.ad.ws.PowerCalculatorImpl does not contain any valid WebMethods. ``` Is creating an service interface absolutely essential in creating a web service? ``` ========================================================================================= ``` Update1: I had missed the public keyword in the method signature: ``` @WebMethod public Double raisedToThePower(Double base, Double power) ``` Also after @kingAm suggestion I removed the endpointInterface so the class looks like: ``` package com.ad.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.ParameterStyle; import javax.jws.soap.SOAPBinding.Style; import javax.jws.soap.SOAPBinding.Use; @WebService @SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED) public class PowerCalculatorImpl { @WebMethod public Double raisedToThePower(Double base, Double power) { return Math.pow(base, power); } } ``` My simple client class looks like: ``` package com.ad.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.ad.ws.PowerCalculatorImpl; public class PowerCalculatorImplClient { public static void main(String[] args) throws Exception { URL url = new URL("http://myhostname.ad.com:8585/ayusman/PowCalc?wsdl"); QName qname = new QName("http://ws.ad.com/", "PowerCalculatorService"); Service service = Service.create(url, qname); PowerCalculatorImpl powerCalculatorImpl = service.getPort(PowerCalculatorImpl.class); System.out.println(powerCalculatorImpl.raisedToThePower(2, 5)); } } ``` Here are the exceptions I see: ``` Exception in thread "main" java.lang.IllegalArgumentException: com.ad.ws.powerCalculatorImpl is not an interface at java.lang.reflect.Proxy.getProxyClass0(Unknown Source) at java.lang.reflect.Proxy.newProxyInstance(Unknown Source) at com.sun.xml.internal.ws.client.WSServiceDelegate$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) ``` It seems that the jax-ws expects an interface here or am I mistaken?