在Client服务中的pom.xml文件中添加依赖
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.2.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.2.5</version> </dependency> </dependencies>
在Server服务中添加日志拦截器
package com.rg2.webservice.impl; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.rg2.webservice.HelloWorld; public class Server { public static void main(String[] args) { System.out.println("web service start"); HelloWorld implementor = new HelloWorldImpl(); String address = "http://localhost/helloWorld"; // Endpoint.publish(address, implementor);//jdk实现暴露webservice接口 JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress(address);//设置暴露地址 factoryBean.setServiceClass(HelloWorld.class);//接口类 factoryBean.setServiceBean(implementor);//设置实现类 factoryBean.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器 factoryBean.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器 factoryBean.create();//创建webservice接口 System.out.println("web service started"); } }
在Client服务中添加日志拦截器
package com.rg2.webservice; import java.util.List; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingInInterceptor; public class Client { public static void main(String[] args) { HelloWorldService service = new HelloWorldService(); HelloWorld helloWorldPort = service.getHelloWorldPort(); org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloWorldPort); client.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器 client.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器 MyRoleArray getroles = helloWorldPort.getroles(); List<MyRole> roleList = getroles.item; for (int i = 0; i < roleList.size(); i++) { MyRole my = roleList.get(i); System.out.print(my.key + ":"); for (Role role : my.value) { System.out.print(role.getId()+","+role.getRoleName()); } System.out.println("==============="); } } }