一、服务端实现。
1.CXF必须包;
2.applicationcontext.xml里面加入
(1)头文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.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" />
(2)暴露接口
/* http://ip:端口/项目名/web.xml配置cxf路径/applicationcontext.xml配置路径*/
/* http://localhost:7001/doubleRecord/recordServices/obtainPolicyInfo?wsdl 查看文档*/
/* http://localhost:7001/doubleRecord/recordServices/列出此路径下所有方法*/
<jaxws:endpoint id="obtainPolicyInfoServer" address="/obtainPolicyInfo">
<jaxws:implementor>
<ref bean="obtainPolicyInfoService"/>//spring中配置class路径
</jaxws:implementor>
</jaxws:endpoint>
3.web.xml程序加载引入CXF
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/recordServices/*</url-pattern>
</servlet-mapping>
<!--CXF配置文件-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/recordServices/*</url-pattern>//CXF路径
</servlet-mapping>
4.需要暴露的接口类用@webservice注解,方法用@webMethod。
service接口:
@WebService(name = "obtainPolicyInfoService", targetNamespace = "http://facade.service.survey.com")
public interface ObtainPolicyInfoService {
@WebMethod(operationName = "getPolicyInfo", action = "")
@WebResult(name = "return", targetNamespace = "")
public String obtainPolicyInfo(@WebParam(name="arg0",targetNamespace = "")ObtainPolicyInfoDomain policyInfo);
}
Service实现类:
@WebService(endpointInterface = "com.sinosoft.survey.service.facade.ObtainPolicyInfoService")
public class ObtainPolicyInfoServiceSpringImpl implements ObtainPolicyInfoService {
/* http://localhost:7001/doubleRecord/recordServices/obtainPolicyInfo/getPolicyInfo */
private static final Log logger = LogFactory.getLog(ObtainPolicyInfoServiceSpringImpl.class);
private PolicyMainInfoService policyMainInfoService;
@WebMethod(operationName = "getPolicyInfo")
@WebResult(name = "result")
public String obtainPolicyInfo(ObtainPolicyInfoDomain policyInfo){
//业务
return "你好!"
}
}
传输对象:全是String类型
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "arg0", propOrder = { "PolicyNo", "certiType","certiNo" })
public class ObtainPolicyInfoDomain implements Serializable {
private String PolicyNo;
private String certiType;
private String certiNo;
public ObtainPolicyInfoDomain(){}
public ObtainPolicyInfoDomain(String policyNo, String certiType, String certiNo) {
PolicyNo = policyNo;
this.certiType = certiType;
this.certiNo = certiNo;
}
//getset方法
}
5.测试:使用soapUI测试 不能使用postman.soap=http+xml,postman只是http
二、客户端实现。
1.启动服务端程序,浏览器输入地址http://localhost:8080/YYY/ewebservice。选择需要调用的接口方法。点击得到wsdl地址
http://localhost:8080/YYY/ewebservice/s2?wsdl。
2.新建工程使用Myeclipse自动生成客户端代码,右键工程选择other,新建webservice client,将wsdl地址输入,finish即可,客户端代码产生好了。写测试类调用方法
public class tttt {
public static void main(String[] args) throws RemoteException,
ServiceException {
StorageHServiceProxy sp=new StorageHServiceProxy();
System.out.println(sp.getT("4"));
}
}