【问题标题】:Publishing a web service with out creating an Interface在不创建接口的情况下发布 Web 服务
【发布时间】:2014-02-25 06:14:23
【问题描述】:

我正在使用 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 服务是否绝对必要?

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

更新1:

我错过了方法签名中的 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 在这里需要一个接口还是我弄错了?

【问题讨论】:

    标签: java web-services wsdl jax-ws


    【解决方案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
    

    是的,你可以。只需在您的实现类中删除 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!");
    
        }
    
    }
    

    请检查您的代码一次,您一定遗漏了什么。

    【讨论】:

    • 是的,你是对的。我在方法签名“@WebMethod public Double raiseToThePower(Double base, Double power)”中遗漏了“public”关键字。但是在进行更改后;当我尝试使用一个简单的客户端时;它仍然会导致问题。请查看我的 OP 编辑​​。
    • 先生,把@Webservice() 放在你的课前。否则 JAX WS 框架将无法将您的实现类识别为服务端点。
    • 如果你看的话;异常说“com.ad.ws.powerCalculatorImpl 不是接口”
    • 我已经根据您的问题使用我在我的电脑上尝试过的实现编辑了我的答案,它工作正常。
    • 您是否在执行客户端代码或发布 powercalculator 服务器时遇到此异常?
    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 2010-10-28
    • 2014-09-11
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多