编程语言
首页 > 编程语言> > java-发布Web服务而无需创建接口

java-发布Web服务而无需创建接口

作者:互联网

我正在使用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在这里需要接口,还是我弄错了?

解决方法:

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

    }

}

请检查一次您的代码,您必须缺少某些内容.

标签:jax-ws,wsdl,web-services,java
来源: https://codeday.me/bug/20191122/2056743.html