/**接口*/ import javax.jws.WebService; @WebService public interface FirstServicer { public String getUserMessage(); }
/**实现类*/ import javax.jws.WebService; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; @WebService public class FirstServiceImpl implements FirstServicer { public String getUserMessage() { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("users"); for (int i = 0; i < 4; i++) { Element user = root.addElement("user"); user.addElement("id").setText(i + ""); user.addElement("password").setText(Math.random() + ""); user.addElement("name").setText("user" + i); } return doc.asXML(); } }
?发布服务:
??????????????
/**这是基于JAXWS API发布的*/ <bean id="userMessage"class="com.demo.ws.server.FirstServiceImpl"> </bean> <jaxws:endpoint id="getUserMessage" implementor="#userMessage" address="/GetMessage"> </jaxws:endpoint> /**这是基于Xfire API发布的*/ <jaxws:server id="surveyWebService" serviceClass="com.demo.ws.server.FirstServiceImpl" address="/SurveyWebService"> <jaxws:serviceBean> <ref bean="surveyService"/> <!-- 要暴露的 bean 的引用 --> </jaxws:serviceBean> </jaxws:server>
?
?
测试:
?
????
/**基于JAXWS的client*/ <bean id="client" class="com.demo.server.HelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.demo.server.HelloWorldImpl" /> <property name="address" value="http://localhost:8088/testcxf/webservices/HelloWorld" /> </bean>
?
/**基于CXF的client的配置*/ <jaxws:client id="surveyServiceClient" serviceClass="com.demo.server.HelloWorld" address="http://localhost:8080/CXF_Spring_Survey/SurveyWebService"/>
?
Java客户端代码:得到消费服务Bean进行方法调用测试就成了。
?
??