在不创建接口的情况下发布Web服务

时间:2014-02-25 06:14:23

标签: java web-services wsdl jax-ws

我正在使用JAX-WS创建Web服务并发布它。 我想知道的是;是否可以在不创建界面的情况下发布Web服务。 意思是,现在我创建一个接口

终点接口类:

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

然后我创建一个接口实现者类:

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

}

在此之后,我使用以下内容发布:

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

}

我想知道的是,我可以将界面和实现者结合成一个;类似的东西:

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

}

当我这样做并启动发布者时,我得到以下堆栈跟踪

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.

在创建Web服务时创建服务接口是绝对必要的吗?

=========================================================================================

UPDATE1:

我错过了方法签名中的public关键字:

@WebMethod public Double raisedToThePower(Double base, Double power)

在@kingAm建议之后,我删除了endpointInterface,所以该类看起来像:

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

    }
}

以下是我看到的例外情况:

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需要一个接口,或者我错了?

1 个答案:

答案 0 :(得分:3)

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

是的,你可以。只需在实现类中删除Webservice注释中的endpointInterface声明。

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

    }

}

请检查一下你的代码,你必须遗漏一些东西。