如果要在tomcat下开发web服务,则必须要下载jaxws.zip,下载地址:http://jax-ws.java.net/2.2.5/
如果要创建一个web服务,则需要按照以下目录方式部署进行:
上图中:
lib放置jaxws.zip中的jar包;
classes放置web服务的SIB和SEI(如果是style=document,则还需要wsgen工具生成类型);
sun-jaxws.xml是web服务特定的配置文件;
我们这里WEBROOT假定是ws文件夹;
package org.xiazdong.ws; import javax.jws.*; @WebService public interface TempConvert{ @WebMethod int f2c(int i); @WebMethod int c2f(int i); }
package org.xiazdong.ws; import javax.jws.*; @WebService(endpointInterface="org.xiazdong.ws.TempConvert") public class TempConvertImpl implements TempConvert{ public int f2c(int i){ return i-5; } public int c2f(int i){ return i+5; } }
javac -d . *.java
wsgen -cp . org.xiazdong.ws.TempConvertImpl
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <servlet-name>TimeServerWS</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TimeServerWS</servlet-name> <url-pattern>/ws</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="TimeServerWS" implementation="org.xiazdong.ws.TempConvertImpl" url-pattern="/ws"/> </endpoints>
最后输入http://localhost:8080/ws/ws?wsdl看是否存在wsdl,如果存在,则说明部署正确;
wsimport -keep -p client http://localhost:8080/ws/ws?wsdl
生成服务的代码;
import client.*; public class TimeClient{ public static void main(String args[]){ TempConvertImplService service = new TempConvertImplService(); TempConvert c = service.getTempConvertImplPort(); System.out.println(c.f2C(5)); } }