I use JAX-WS to create a web service and publish it. What I wanted to know Is it possible to publish a web service without creating an interface. Sense, right now I'm creating an interface
Endpoint 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 an interface constructor 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 that, I post 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());
}
}
I would like to know if I can combine both the interface and implemntor into one; sort of:
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 run 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.
Does the interface create the services absolutely necessary to create a web service?
Update1:
I missed the public keyword in the method signature:
@WebMethod public Double raisedToThePower(Double base, Double power)
Also, after the @kingAm suggestion, I deleted endpointInterface, so the class looks like this:
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 this:
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 that 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)
, jax-ws ?