一.服务端搭建
1.所需要的jar包
2.方法定义
package com.zst.gdtbase.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @Auther: pyl
* @Date: 2018/5/18/018 20:45
* @Description:
*/
@WebService
public interface EnterpriseArchitecture {
@WebMethod
public String receive(@WebParam(name="arg0",targetNamespace ="http://webservice.gdtbase.zst.com/")String uniqueId,
@WebParam(name="arg1",targetNamespace ="http://webservice.gdtbase.zst.com/")String sysCode,
@WebParam(name="arg2",targetNamespace ="http://webservice.gdtbase.zst.com/")String distributedData);
}
3.方法实现
package com.zst.gdtbase.webservice;
import javax.jws.WebService;
/**
* @Auther: pyl
* @Date: 2018/5/18/018 20:51
* @Description:
*/
public class EnterpriseArchitectureImpl implements EnterpriseArchitecture {
@Override
public String receive(String uniqueId,String sysCode,String distributedData) {
System.out.println("uniqueId:"+uniqueId);
System.out.println("--------------------------");
System.out.println("sysCode:"+sysCode);
System.out.println("-----------------------");
System.out.println("distributeData:"+distributedData);
return "接口返回数据";
}
}
4.修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>zstTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/service-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.创建service-bean.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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<bean id="SayHello" class="com.zst.gdtbase.webservice.EnterpriseArchitectureImpl" />
<jaxws:server id="enterpriseArchitecture" serviceClass="com.zst.gdtbase.webservice.EnterpriseArchitecture" address="/SayHello">
<jaxws:serviceBean>
<ref bean="SayHello"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
配置完成,启动服务器,在浏览器访问
http://localhost:8080/项目名/SayHello?wsdl
出现以下画面说明搭建成功
二,服务端
1.代码配置请求
package com.zst.gdtbase.webservice;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class WebserviceClient {
public static void main(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(EnterpriseArchitecture.class);
factory.setAddress("http://localhost:8080/zstTest/SayHello");
EnterpriseArchitecture service = (EnterpriseArchitecture) factory.create();
System.out.println("#############Client getUserByName##############");
String result = service.receive("1", "1" ,"1");
System.out.println("result:"+result);
}
}
2.xml配置请求
创建service-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxws:client id="userWsClient" serviceClass="com.zst.gdtbase.webservice.EnterpriseArchitecture"
address="http://localhost:8080/zstTest/sayHello" />
</beans>
用service-client.xml请求
package com.zst.gdtbase.webservice;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WebserviceClient {
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("service-client.xml");
EnterpriseArchitecture serviceClient= context.getBean(EnterpriseArchitecture.class);
System.out.println("#############Client getUserByName##############");
String result1 = serviceClient.receive("1", "1" ,"1");
System.out.println("result:"+result1);
}
}
注意:service-client文件需要放到src/main/resource下面才能正常读取
3.请求参数注意
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<Esb soapenv:actor="" soapenv:mustUnderstand="0">
<Route>
<Sender>cpi_mdm_test</Sender>
<ServCode>cpi_cloud_test.clouddata.SynReq</ServCode>
<MsgId />
<TransId />
</Route>
</Esb>
</soapenv:Header>
<soapenv:Body>
<ns1:receive xmlns:ns1="http://webservice.gdtbase.zst.com/"
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<ns1:arg0 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="soapenc:string">
780C8A25B681458681F751968EF586E1
</ns1:arg0>
<ns1:arg1 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="soapenc:string">
YDPTYZZ
</ns1:arg1>
<ns1:arg2 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="soapenc:string">
111111111111111
</ns1:arg2>
</ns1:receive>
</soapenv:Body>
</soapenv:Envelope>
注意:<soapenv:Body>标签下的ns1命名空间需要与接口参数定义的命名空间一致