webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录。
1.Axis2
Axis是apache下一个开源的webservice开发组件,出现的算是比较早了,也比较成熟。这里主要介绍Axis+eclipse开发webservice,当然不用eclipse也可以开发和发布webservice,只是用eclipse会比较方便。
(1)下载eclipse的Java EE版本
http://www.eclipse.org/downloads/
(2)下载axis2
http://axis.apache.org/axis2/java/core/download.cgi
(3)下载eclipse的axis2插件
Axis2_Codegen_Wizard
Axis2_Service_Archiver
http://axis.apache.org/axis2/java/core/tools/index.html
推荐使用1.3的版本
(4)eclipse安装axis2插件
1)在任意目录下新建一个Axis2文件夹,在该文件夹下新建eclipse目录,在eclipse目录中新建plugins目录和features目录,例如:D:\programSoftware\eclipse-SVN\Axis2\eclipse;
2)把下载的axis2插件解压,并把解压的文件放到新建的eclipse的plugins目录下;
3)在%eclipse_home%的目录下新建links目录,并在links目录下新建axis2.link文件,内容为:path=D:\programSoftware\eclipse-SVN\Axis2;
4)重启eclipse,点击·file-new-other,如果看到Axis2 Wizards,则表明插件安装成功。
(5)安装axis2
下载Axis2的WAR Distribution并解压,把axis2.war包放置到%TOMCAT_HOME%/webapps下,启动tomcat,访问http://localhost:port/axis2,Axis2安装成功。
(6)使用eclipse新建web工程,创建一个普通java类,至少包含一个方法。
(7)发布webservice
1)点击eclipse的File-New-other,打开Axis2 Wizards,选择Axis2 Service Archiver,然后Next;
2)选择Class File Location,也就是类文件存放路径,注意:只选到classes目录,不要包括包文件夹,然后Next;
3)选择Skip WSDL,然后Next
4)一路Next到Select the Service XML file to be included in the Service archive,勾选Generate theservice xml automatically;
5)Service Name-填写你的service名称,Class Name-填写类名称,要包括包名,然后点击load,然后点击Finish,这时webservice就发布成功了;
6)然后到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services 看看是否多了一个.aar的文件;
7)访问http://localhost:8085/axis2/services/类名?wsdl 就可看到生成的wsdl文件了。
注意:以上的方式是发布到axis2.war包中,你也可以把生成.aar文件copy到你的实际应用中,同时,你也可以使用eclipse的create webservice功能发布你的webservice,选择axis2生成你的webservice,这样webservice就会部署到你的应用中了。
2.Apche CXF
CXF开发webservice也是比较方便和简单的,它和spring的集成可以说是非常地好。举一个CXF开发webservice的例子吧。
1)在eclipse中新建一个web工程,导入依赖包,如图:
2)编写一个接口,如:
public String test(@WebParam(name="value", targetNamespace = "http://service.cxf.zcl.com/", mode = WebParam.Mode.IN)String value);
注意:CXF开发的webservice,接口中的方法的参数一定要以这种方式,否则客户端调用的时候CXF服务端会接收不到参数的值,name:参数名称,可不写(建议写上),targetNamespace:命名空间,一定要填写上,默认是包名反过来的顺序,mode:参数类型,IN表示输入。
3)编写一个实现类,实现接口的方法;
4)和spring的集成,编写一个bean文件,如:cxf-beans.xml,内容如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />
- </beans>
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />
</beans>
这个文件比较容易理解,就不解释了。
5)配置CXFServlet
在web.xml文件中配置CXFServlet,加载cxf-beans.xml文件,内容如下:
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/cxf-beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>cxf</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>cxf</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
把工程部署到中间件,如tomcat,就可以访问该webservice了。
3.JDK开发webservice
1)编写一个Java类,如下:
- package demo;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import javax.xml.ws.Endpoint;
- @WebService
- public class JdkWebService {
- public String doSomething(@WebParam(name="value", targetNamespace = "http://demo/", mode = WebParam.Mode.IN)String value) {
- return "Just do it," + value + "!";
- }
- public static void main(String[] args) {
- Endpoint.publish("http://localhost:8080/jdkwsdemo/demo.JdkWebService", new JdkWebService());
- }
- }
package demo;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class JdkWebService {
public String doSomething(@WebParam(name="value", targetNamespace = "http://demo/", mode = WebParam.Mode.IN)String value) {
return "Just do it," + value + "!";
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/jdkwsdemo/demo.JdkWebService", new JdkWebService());
}
}
2)运行该java类,在浏览器上就可以访问该webservice了。
注意:开发web工程的时候,这种方法不太友好。我们可以编写一个servlet类,在servlet类的初始化方法中发布webservice,这样我们的中间件服务器启动的时候就会帮我们自动webservice了。
4) xfire
1.新建一个java web project命名为TestWebService,将xfire相关的jar包添加到lib目录中,写接口类和实现类
- package com.lamp.service;
- public interface MessageService {
- public String getName(String name);
- }
package com.lamp.service;
public interface MessageService {
public String getName(String name);
}
实现类
- package com.lamp.service.impl;
- import com.lamp.service.MessageService;
- public class MessageServiceImpl implements MessageService {
- public String getName(String name) {
- return "hellow " + name + ", welcome to WebService world";
- }
- }
package com.lamp.service.impl;
import com.lamp.service.MessageService;
public class MessageServiceImpl implements MessageService {
public String getName(String name) {
return "hellow " + name + ", welcome to WebService world";
}
}
在src目录下新建文件夹META-INF,然后再在其下新建文件夹xfire,在xfire目录下新建配置文件services.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>MessageService</name>
- <serviceClass>com.lamp.service.MessageService</serviceClass>
- <implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass>
- </service>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>MessageService</name>
<serviceClass>com.lamp.service.MessageService</serviceClass>
<implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass>
</service>
</beans>
最后在web.xml中配置xfire的servlet
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>
- org.codehaus.xfire.transport.http.XFireConfigurableServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/servlet/XFireServlet/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
项目部署后在浏览器中通过http://localhost:8080/TestWebService/services访问看到远程访问的接口,并得到wsdl为http://localhost:8080/TestWebService/services/MessageService?wsdl
这样服务器端开发完毕,现在开始客户端的开发
新建一个java project也将xfire相关的jar引入,我用ant在客户端生成代理对象,在项目路径下新建build.xml,代码为
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="WebService" basedir="." default="gen-webservice">
- <property file="build.properties">
- </property>
- <path id="project-classpath">
- <fileset dir="${lib.dir}">
- <include name="**/*.jar" />
- </fileset>
- </path>
- <target name="gen-webservice">
- <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" />
- <wsgen outputDirectory="${src.dir}"
- wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/>
- </target>
- </project>
<?xml version="1.0" encoding="UTF-8"?>
<project name="WebService" basedir="." default="gen-webservice">
<property file="build.properties">
</property>
<path id="project-classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="gen-webservice">
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" />
<wsgen outputDirectory="${src.dir}"
wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/>
</target>
</project>
其引入的build.properties文件也在项目路径下
src.dir=${basedir}/src
lib.dir=F:/WebService/xfire-1.2.6/lib
wsdl.dir=http://localhost:8080/TestWebService/services/MessageService?wsdl
其中lib.jar为我存放xfire的路径,运行ant得到代理对象
编写一个测试类
- package com.lamp.test;
- import com.lamp.ws.client.MessageServiceClient;
- import com.lamp.ws.client.MessageServicePortType;
- public class TestGetName {
- public static void main(String[] args) {
- MessageServiceClient msg = new MessageServiceClient();
- MessageServicePortType portType = msg.getMessageServiceHttpPort();
- String result = portType.getName("张三");
- System.out.println(result);
- }
- }
package com.lamp.test;
import com.lamp.ws.client.MessageServiceClient;
import com.lamp.ws.client.MessageServicePortType;
public class TestGetName {
public static void main(String[] args) {
MessageServiceClient msg = new MessageServiceClient();
MessageServicePortType portType = msg.getMessageServiceHttpPort();
String result = portType.getName("张三");
System.out.println(result);
}
}
运行在控制台看到了hellow 张三, welcome to WebService world至此一个简单的WebService开发完毕