web服务
javaEE为web service提供了两种不同的编程模型,EJB容器模型及WEB容器模型,这里将以最新的JAX-WS2.x规范(java api for xml-based
services)介绍webservice的开发
一:一个生成webservice的java源文件:
package com.oristand.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebService;
import com.oristand.bean.Order;
import com.oristand.service.OrderService;
@WebService(targetNamespace="http://ws.oristand.cn",
name="OrderService",
serviceName="OrderServiceBean"
)
@Stateless
@Remote(OrderService.class)
public class OrderServiceBean implements OrderService {
public Order getOrder(String orderid) {
Order order = new Order();
order.setName("赵一");
order.setOrderid(orderid);
return order;
}
public List<Order> getOrders() {
List<Order> person = new ArrayList<Order>();
Order order1 = new Order();
order1.setName("赵一");
order1.setOrderid("111");
person.add(order1);
Order order2 = new Order();
order2.setName("赵一");
order2.setOrderid("222");
person.add(order2);
return person;
}
public String getUserName(String name) {
// TODO Auto-generated method stub
return name;
}
}
然后就可以通过http://127.0.0.1:8080/WebService/OrderServiceBean?wsdl来查看发布的该webservice服务的相关信息
二:webservice客户端的编写
这里是通过build.xml来生成一系列的.java原文件:
<project name="WSClient" default="wsclientgen" basedir=".">
<property name="src.dir" value="${basedir}/src"/>
<!-- Build classpath -->
<path id="build.classpath" description="build classpath">
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<!-- ================================================================== -->
<!-- Generating sources -->
<!-- ================================================================== -->
<target name="wsclientgen" description="generate source">
<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport" classpathref="build.classpath"/>
<wsimport wsdl="http://127.0.0.1:8080/WebService/OrderServiceBean?wsdl"
sourcedestdir="${src.dir}" package="cn.oristand.ws.client" keep="true"
verbose="true" destdir="${basedir}/bin"/>
</target>
</project>
在运行这个build.xml的时候需要引入JAXWS2.1.7-20090419.jar这个文件,用到这里面的一些.jar文件。