gpt4 book ai didi

java - 在不创建接口(interface)的情况下发布 Web 服务

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:07 25 4
gpt4 key购买 nike

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

端点接口(interface)类:

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

然后我创建一个接口(interface)实现类:

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

}

我想知道的是,能否将接口(interface)和实现器合二为一;像这样的东西:

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.

创建服务接口(interface)对于创建 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)情况:

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 需要一个接口(interface),还是我弄错了?

最佳答案

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

}

}

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

关于java - 在不创建接口(interface)的情况下发布 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22006092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com