webservice是SOA(面向服务编程)的一种实现,
主要是用来实现异构平台通信也就
是不同平台不同项目之间的数据传输,从而避免了信息孤岛的问题,
它之所以能够
进行异构平台通信是因为它是完全基于xml的,
所以说,webService是跨平台,
跨语言,跨框架的,在java中通常有三种技术框架分别是xfire,cxf,axis2。
我们为了保证
webservice的安全性,采用了基于
WS-Security标准的安全验证(使用回调函数)。
服务端的配置
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext.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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping>
applicationContext.xml配置
<!-- 这三行的配置不用去检查对应的路径下是否有对应的文件,因为cxf会自动生成的--> <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" /> <bean id="userServiceBean" class="com.pengbw.service.TwebServiceImpl"/> <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <!-- 服务端配置 address客户端要用到--> <jaxws:server id="userService" serviceClass="com.pengbw.service.TwebService" address="/user"> <jaxws:serviceBean> <ref bean="userServiceBean"/> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="inMessageInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="outLoggingInterceptor"/> </jaxws:outInterceptors> </jaxws:server>
接口定义
@WebService
public interface TwebService {
public String user(String name);
}
实现类
@WebService //可有可无
public class TwebServiceImpl implements TwebService {
/* (non-Javadoc)
* @see com.pengbw.service.TwebService#user(java.lang.String)
*/
@Override
public String user(String name) {
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
System.out.println(name);
return name;
}
}
地址访问
http://127.0.0.1:8083/webService/cxf/user?wsdl
配置成功
客户端的配置
环境配置 我用的是(apache-cxf-3.0.2)
打开cmd
wsdl2java -p com.org.service -d "d:\test" "http://127.0.0.1:8083/webService/cxf/user?wsdl"
com.org.service:生成文件夹结构
d:\test:保存地址
http://127.0.0.1:8083/webService/cxf/user?wsdl:访问服务器路径
---------------------->生成文件
新建客户端
配置文件 applicationContext.xml
<!-- 这三行的配置不用去检查对应的路径下是否有对应的文件,因为cxf会自动生成的--> <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:client id="userWsClient" serviceClass="com.pengbw.servic.TwebService" address="http://127.0.0.1:8083/webService/cxf/user"/>
将生成的文件夹复制到客户端src下
测试
public class TestService {
@Test
public void sayHello() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
TwebService service = (TwebService) ctx.getBean("userWsClient");
String hello = service.user("BBBBBBBBB");
}
}
结果