首先在一个logicContext.xml中进行如下配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"
default-autowire="byName" default-lazy-init="false">
<!--2010-06-22 lj添加WebService配置-->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/StarInforDealService">
<ref bean="starInforDealServiceBean" />
</entry>
</map>
</property>
</bean>
<bean id="starInforDealServiceBean" class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
<property name="serviceBean" ref="starInforDealServiceImpl" />
<property name="serviceClass" value="com.ncs.bureausec.webservice.service.StarInforDealService" />
</bean>
</beans>
接下来是写一个client端
import java.net.URL;
import java.util.List;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ncs.bureausec.common.PropertiesReader;
import com.ncs.bureausec.icp.po.AutoStarAccess;
public class StarInforClient {
private static Logger log = LoggerFactory.getLogger(WebsitePropertyCategoryClient.class);
public static boolean sendInfor(String jsonarray){
String url = PropertiesReader.getInstance().getProperty("SendInfor_URL");
Client client = null;
Object[] results = null;
try {
client = new Client(new URL(url));
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
results = client.invoke("insertStarInfor", new Object[] { jsonarray });
} catch (Exception e) {
log.error("下发失败");
e.printStackTrace();
return false;
}
return true;
}
}
接下来是SendInfor_URL的在一个property中配置
SendInfor_URL=http://127.0.0.1:8080/icpproject_state_star/services/StarInforDealService?wsdl
最后是服务器端,首先写个接口
import java.util.List;
import com.ncs.bureausec.icp.po.AutoStarAccess;
public interface StarInforDealService {
public boolean insertStarInfor(String jsonarray);
}
接口的实现
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ncs.bureausec.icp.dao.AutoStarAccessDao;
import com.ncs.bureausec.icp.po.AutoStarAccess;
import com.ncs.bureausec.webservice.service.StarInforDealService;
@Service("starInforDealServiceImpl")
public class StarInforDealServiceImpl implements StarInforDealService {
@Autowired
private AutoStarAccessDao autoStarAccessDao;
@Override
public boolean insertStarInfor(String jsonarray) {
// TODO Auto-generated method stub
JSONArray jarray = JSONArray.fromObject(jsonarray);
List<AutoStarAccess> list = (List<AutoStarAccess>) JSONArray.toList(jarray,AutoStarAccess.class);
for(int i=0;i<list.size();i++){
AutoStarAccess autoStarAccess = list.get(i);
autoStarAccess.setDataTime(new Date());
System.out.println(autoStarAccess.getIspName());
// this.autoStarAccessDao.insertAutoStarAccess(autoStarAccess);
}
return true;
}
}
注意webservice在client端到service传参时,只能是一些简单的类型,如int ,String等