学习webservice之cxf:cxf日志拦截器
Posted 十黎九夏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习webservice之cxf:cxf日志拦截器相关的知识,希望对你有一定的参考价值。
在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("==============="); } } }
以上是关于学习webservice之cxf:cxf日志拦截器的主要内容,如果未能解决你的问题,请参考以下文章
WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)