package rece.test;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceContext;
import com.sun.net.httpserver.HttpExchange;
@WebService(targetNamespace = "http://rece.test.com", serviceName = "WebServiceRece")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class WebServiceRece
{
@Resource
private WebServiceContext wsContext;
@WebMethod
public String sendZxdxSms(@WebParam(partName = "Username")String userName,
@WebParam(partName = "Password")String passWord,
@WebParam(partName = "Content")String content,
@WebParam(partName = "FromTerminalId")String fromTerminalId,
@WebParam(partName = "ToTerminalId")String toTerminalId
)
{
//获取提交请求的IP地址
HttpExchange exchange = (HttpExchange)wsContext.getMessageContext().get("com.sun.xml.internal.ws.http.exchange");
String remortAddress = exchange.getRemoteAddress().getAddress().getHostAddress();
//具体实现
return WebServiceReceImpl.getInstance().send(userName, passWord, content, fromTerminalId, toTerminalId,remortAddress);
}
}
在main方法中启动这个webservice服务:
Endpoint.publish("http://localhost:9898/test/send.jws", new WebServiceRece());
打开wsdl
http://localhost:9898/test/send.jws?wsdl
生成客户端
在命令行下运行wsimport –keep http://localhost:9898/test/send.jws?wsdl,即可在当前目录生成客户端,-keep表示保留源文件,如果没有-keep,则只生成class文件
本文介绍了一个基于Java的WebService服务实现短信发送的具体代码示例。该服务使用了SOAP绑定风格并实现了从远程终端ID到接收终端ID的短信发送功能。通过HttpExchange获取客户端IP地址,并调用WebServiceReceImpl类完成实际的短信发送过程。
497

被折叠的 条评论
为什么被折叠?



