您的位置:首页 > 编程语言 > Java开发

JAVA随笔篇四(读懂WebService过程)

2015-09-11 14:41 288 查看
Web Service 是一个能够实现远程数据交互的一个技术和协议,通过HTML进行通讯。 他实现了 不同系统不同平台,不同开发语言和开发技术实现的软件系统之间的通讯。

Web Service大体上分为5个层次: 
1. Http传输信道 
2. XML的数据格式 
3. SOAP封装格式 
4. WSDL的描述方式 
5. UDDI

Web
Service 分为服务端和客户端,服务端提供了调用方法的具体实现,客户端声明了方法但并不实现。

下面转载一些代码说明问题:来源:http://www.iteye.com/problems/31567

服务端的程序:

public interface SayHello {
public void helloworld(String name);
}

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.BindingType;

import com.sun.xml.internal.ws.api.SOAPVersion;

@Stateless

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class SayHelloBean implements SayHello {
@WebMethod
public void helloworld(String name) {

System.out.println("hello world");
}

}


这里只是一个WebService,需要发布出去,

Endpoint.publish(publishUrl, new WebService());

利用这条语句发布WebService,http://www.iteye.com/topic/1135747这篇文章讲了简单的WebService搭建方法。

当服务器端启动之后,在客户端需要建立一个WebServiceClient和一个WebService。

WebService:

@WebService(name = "SayHelloBean", targetNamespace = "http://HELLO.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC</span>)
public interface SayHelloBean {

/**
*
* @param arg0
*/
@WebMethod
public void helloworld(
@WebParam(name = "arg0", partName = "arg0") String arg0);

}
WebServiceClient:
@WebServiceClient(name = "SayHelloBeanService", targetNamespace = "http://HELLO.com/", wsdlLocation = "http://127.0.0.1:8080/ejb4Webservice/SayHelloBean?wsdl")
public class SayHelloBeanService extends Service {

private final static URL SAYHELLOBEANSERVICE_WSDL_LOCATION;
private final static Logger logger = Logger
.getLogger(com.HELLO.SayHelloBeanService.class.getName());

static {
URL url = null;
try {
URL baseUrl;
baseUrl = com.HELLO.SayHelloBeanService.class.getResource(".");
url = new URL(baseUrl,
"http://127.0.0.1:8080/ejb4Webservice/SayHelloBean?wsdl");
} catch (MalformedURLException e) {
logger
.warning("Failed to create URL for the wsdl Location: 'http://127.0.0.1:8080/ejb4Webservice/SayHelloBean?wsdl', retrying as a local file");
logger.warning(e.getMessage());
}
SAYHELLOBEANSERVICE_WSDL_LOCATION = url;
}

public SayHelloBeanService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}

public SayHelloBeanService() {
super(SAYHELLOBEANSERVICE_WSDL_LOCATION, new QName(
"http://HELLO.com/", "SayHelloBeanService"));
}

/**
*
* @return returns SayHelloBean
*/
@WebEndpoint(name = "SayHelloBeanPort")
public SayHelloBean getSayHelloBeanPort() {
return super.getPort(new QName("http://HELLO.com/",
"SayHelloBeanPort"), SayHelloBean.class);
}

public static void main(String[] args) {
SayHelloBeanService sh =new SayHelloBeanService();
SayHelloBean s = sh.getSayHelloBeanPort();
s.helloworld("asdasdsada");
}

}

在这里,需要注意,
targetNamespace

 
指定从 Web Service 生成的 WSDL 和 XML 元素的 XML 名称空间。缺省值为从包含该 Web Service 的包名映射的名称空间。(字符串)

wsdlLocation
指定用于定义 Web Service 的 WSDL 文档的 Web 地址。Web 地址可以是相对路径或绝对路径。(字符串)
进入wsdl页面,看到xml内容则说明发布成功。SOAPBinding

简单对象访问协议,主要有PRC、DOCUMENT两种style。不同可以参考以下链接。
http://www.micmiu.com/soa/webservice/jdk6-webservice-doc-rpc/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web service java