这篇文章介绍一下如何在java中发布WEB SERVICE
在java程序发布web serice 可以使用几种开源的产品如:
AXIS,XFIRE,CXF。现在CXF应该可以取代XFIRE作为首选的WEB SERVICE 发布产品。
下面介绍一下如何使用CXF发布一个web service。
1.下载CXF。
在 http://cxf.apache.org/ 下载cxf软件。
2.新建一个web项目。
添加如下包的引用。
代码
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
spring 的包
aopalliance-1.0.jar
spring-core-2.0.8.jar
spring-beans-2.0.8.jar
spring-context-2.0.8.jar
spring-web-2.0.8.jar
cxf 的包
cxf-2.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
spring 的包
aopalliance-1.0.jar
spring-core-2.0.8.jar
spring-beans-2.0.8.jar
spring-context-2.0.8.jar
spring-web-2.0.8.jar
cxf 的包
cxf-2.1.jar
这个包可能跟使用的cxf版本有差异。
修改 web.xml
代码
<!-- Spring Config Location -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Apache CXFServlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<!--
<display-name>CXF Servlet</display-name>
-->
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- CXFServlet Mapping -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Apache CXFServlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<!--
<display-name>CXF Servlet</display-name>
-->
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- CXFServlet Mapping -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
3.编写webservice。
开发一个web服务接口。接口代码如下:
代码
package com.gzepb.motor;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace="http://www.xxx.com/")
public interface IServer {
public String hello(@WebParam(name="name") String name);
}
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(targetNamespace="http://www.xxx.com/")
public interface IServer {
public String hello(@WebParam(name="name") String name);
}
实现类如下:
代码
package com.gzepb.motor.impl;
import com.gzepb.motor.IServer;
public class ServiceImpl implements IServer {
public String hello(String name) {
// TODO Auto-generated method stub
return "hello :" + name;
}
}
import com.gzepb.motor.IServer;
public class ServiceImpl implements IServer {
public String hello(String name) {
// TODO Auto-generated method stub
return "hello :" + name;
}
}
下面的工作就是配置这个web service了
代码
<?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-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import Apache CXF Bean Definition -->
<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"/>
<!-- gzepbmotorService -->
<bean id="ServerImpl" class="com.gzepb.motor.impl.ServiceImpl">
</bean>
<!-- Expose SurveyWebService -->
<jaxws:server id="motorService"
serviceClass="com.gzepb.motor.IServer"
address="/motorService">
<jaxws:serviceBean>
<ref bean="ServerImpl"/> <!-- 要暴露的 bean 的引用 -->
</jaxws:serviceBean>
</jaxws:server>
</beans>
<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-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import Apache CXF Bean Definition -->
<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"/>
<!-- gzepbmotorService -->
<bean id="ServerImpl" class="com.gzepb.motor.impl.ServiceImpl">
</bean>
<!-- Expose SurveyWebService -->
<jaxws:server id="motorService"
serviceClass="com.gzepb.motor.IServer"
address="/motorService">
<jaxws:serviceBean>
<ref bean="ServerImpl"/> <!-- 要暴露的 bean 的引用 -->
</jaxws:serviceBean>
</jaxws:server>
</beans>
这里使用了spring的配置。这样web服务我们就发布好了。