使用的cxf包版本为 cxf-2.6.1.jar 官方下载地址:CXF下载地址
web.xml配置
<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>/ws/*</url-pattern>
</servlet-mapping>
spring 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.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" />
<bean id="webServicesAgent" class="org.apache.cxf.spring.remoting.Jsr181HandlerMapping">
<property name="urlPrefix"><value>/</value></property>
</bean>
</beans>
定义接口(代码片段)
@WebService
public interface MemberService {
/**
* Function:获取企业账号信息
* @author **
* @param memId
* @return
*/
public Member getMemAccount(int memId);
}
接口实现(代码片段) 数据开启了GZIP压缩
@Component
@WebService(serviceName = "MemberService", endpointInterface = "com.webservice.member.service.MemberService")
@SOAPBinding(style = Style.RPC)
@org.apache.cxf.interceptor.InInterceptors (interceptors = {"org.apache.cxf.transport.common.gzip.GZIPInInterceptor","org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" })
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberDao memberDao;
@Override
public Member getMemAccount(int memId) {
return memberDao.getMemAccount(memId);
}
}
访问地址 http://localhost:8080/ws/MemberService?wsdl
客户端调用(定义一个BaseService类)
public class BaseService {
public Object getService(String path[]){
try{
Class<?> c=Class.forName(path[0]);
String address = path[1].replace("{0}",SiteProperty.getSiteSession().serviceUrl);
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(c);
factory.setAddress(address);
Object obj = factory.create();
Client client = ClientProxy.getClient(obj);
Endpoint endpoint = client.getEndpoint();
endpoint.getInInterceptors().add(new GZIPInInterceptor());
endpoint.getOutInterceptors().add(new GZIPOutInterceptor());
return obj;
}catch(Exception e){
//e.printStackTrace();
}
return null;
}
}
客户端Service实现类,继承BaseService(片段)
@Service("memberManagerService")
public class MemberManagerServiceImpl extends BaseService implements MemberManagerService{
/**
* 企业管理Service
*/
public static final String MEMBER_SERVICE[]={"com.webservice.member.service.MemberService","http://{0}/ws/MemberService?wsdl"};
private MemberService getMemberService(){
service =(MemberService)getService(Constants.SERVICE.MEMBER_SERVICE);
return service;
}
/**
* Function:获取企业账号信息
* @author **
* @param memId
* @return
*/
public MemberLogin getMemAccount(int memId){
return getMemberService().getMemAccount(memId);
}
}
时间: 2024-10-01 16:04:22