Publish a web service without creating an interface

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 ?

+3
1
Is creating an service interface absolutely essential in creating a web service?

, .

What I wanted to know is, can I combine both the interface and the implemntor into one

, . endpointInterface Webservice .

@WebService(endpointInterface="com.ad.ws.PowerCalculatorImpl")

, ,

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);
    }

}

, .

, .

,

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;
}

  }

,

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!");

    }

}

, , - .

+3

All Articles