服务端设计 spring <?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-autowire="byName" default-lazy-init="true"> <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="cmsServiceImpl" class="com.sky.applist20.cms.webservice.impl.CmsServiceImpl"> <property name="provinceDAO" ref="bpsProvinceDAO" /> <property name="provinceChannelFileDAO" ref="provinceChannelFileDAO" /> </bean> <bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean"> <property name="wrapped" value="true" /> </bean> <jaxws:endpoint id="cmsService" implementor="#cmsServiceImpl" address="/cmsService"> <jaxws:serviceFactory> <ref bean="jaxWsServiceFactoryBean" /> </jaxws:serviceFactory> </jaxws:endpoint> </beans> 接口类 import javax.jws.WebService; @WebService public interface ICmsService { int syncChannelFile(String provinceCode, byte[] fileContent, String operator); } 接口实现类 @WebService(endpointInterface = "com.sky.applist20.cms.webservice.ICmsService") public class CmsServiceImpl implements ICmsService { public int syncChannelFile(String provinceCode, byte[] fileContent, String operator) { } } 客户端测试 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd" default-autowire="byName" default-lazy-init="true"> <bean id="client" class="com.sky.applist20.cms.webservice.ICmsService" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.sky.applist20.cms.webservice.ICmsService" /> <property name="address" value="http://localhost:8080/cms/ws/cmsService" /> </bean> </beans> 测试类 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sky.applist20.cms.DbUnitHelper; import com.sky.applist20.cms.webservice.ICmsService; /** * @author jin lingmin * */ public class ICmsServiceTestCase { protected final Log logger = LogFactory.getLog(getClass()); protected DbUnitHelper dbUnitHelper = new DbUnitHelper(); private ICmsService client; @Before public void setUp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "spring/applicationContext-client-webservice.xml" }); client = (ICmsService) context.getBean("client"); } @Test public void testSyncChannelFile(){ String filename = "C:\\home\\test\\applist20\\dist\\applist20\\cms\\smsChannelTemp\\" + "100000"; try { File file = new File(filename); byte[] content=readFile(file); int flag = client.syncChannelFile("310000", content, "admin"); assert(flag == 4); } catch (FileNotFoundException e) { logger.error("err:",e); } catch (Exception e) { // TODO Auto-generated catch block logger.error("err:",e); } } /** *//**读文件到字节数组 * @param file * @return * @throws Exception */ static byte[] readFile(File file) throws Exception { if (file.exists() && file.isFile()) { long fileLength = file.length(); if (fileLength > 0L) { BufferedInputStream fis = new BufferedInputStream( new FileInputStream(file)); byte[] b = new byte[(int) fileLength]; while (fis.read(b)!= -1) { } fis.close(); fis = null; return b; } } else { return null; } return null; } /** *//**将字节数组写入文件 * @param filePath * @param content * @return * @throws IOException */ static boolean writeBytes(String filePath, byte[] content) throws IOException { File file = new File(filePath); synchronized (file) { BufferedOutputStream fos = new BufferedOutputStream( new FileOutputStream(filePath)); fos.write(content); fos.flush(); fos.close(); } return true; } }
WebService学习笔记——cxf示例
最新推荐文章于 2019-02-03 23:56:00 发布