java webservice应用总结
step1:在工程中导入xfire-all.jar(这是xfire需要的class包)以及对方的class包
step2: 在工程的src/META-INF下面新建文件夹xfire,在里面添加service.xml文件
其中内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>LetterFeedback</name>
<serviceClass>com.megait.xzxk.webservice.IXzxkService</serviceClass>
<implementationClass>com.cicro.project.shfgw.ws.service.impl.LetterFeedbackService</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>LetterFeedback</name>
<serviceClass>com.megait.xzxk.webservice.IXzxkService</serviceClass>
<implementationClass>com.cicro.project.shfgw.ws.service.impl.LetterFeedbackService</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
以上是配置service接口以及实现类
下面是Java代码:
public class LetterFeedbackService implements IXzxkService {
private static final Log log = LogFactory.getLog(LetterFeedbackService.class);
// 领导信箱: model_id = 1
// pur_id与tbmail.type映射
public static final Map<Integer, Integer> PURID2TYPE_MAPPING = new HashMap<Integer, Integer>();
static {
PURID2TYPE_MAPPING.put(10, 347);
PURID2TYPE_MAPPING.put(12, 352);
PURID2TYPE_MAPPING.put(2, 375);
PURID2TYPE_MAPPING.put(4, 0);
}
@Override
public List<StandardInfo> createObjList() throws AhDataException {
if (log.isInfoEnabled()) {
log.info("Letter Feedback Service Create start...");
}
List<StandardInfo> list = new ArrayList<StandardInfo>();
List<XSqBean> sqList = XSqManager.extractSqList();
StandardInfo info = null;
Tbmail tbmail = null;
Integer tbmailType = null;
if (sqList != null && !sqList.isEmpty()) {
for (SQBean sq : sqList) {
if (log.isInfoEnabled()) {
log.info("sq_id: " + sq.getSq_id());
}
tbmailType = PURID2TYPE_MAPPING.get(sq.getPur_id());
tbmail = new Tbmail();
tbmail.setId(sq.getSq_id());
tbmail.setName(sq.getSq_realname());
tbmail.setMail(sq.getSq_email());
tbmail.setAdr(sq.getSq_address());
tbmail.setTel(sq.getSq_phone());
tbmail.setFax("");
tbmail.setOther("");
tbmail.setType(tbmailType != null ? tbmailType.intValue() : 0);
tbmail.setTitle(sq.getSq_title2());
tbmail.setContent(sq.getSq_content2());
tbmail.setHf("");
tbmail.setHf1("");
tbmail.setHfqk("");
try {
tbmail.setDatetime(DateUtil.getDate(sq.getSq_dtime()));
} catch (ParseException e) {
if (log.isErrorEnabled()) {
log.error(e.toString());
}
}
tbmail.setLb("");
info = new StandardInfo();
info.setObjectInfo(encodeTbmail(tbmail));
info.setTitle(tbmail.getTitle());
info.setBusinesstype("上海发改委来信反馈");
info.setDetail(tbmail.getContent());
info.setBusinessresult("");
list.add(info);
XSqManager.updateSqExtracted(sq.getSq_id());
}
}
if (log.isInfoEnabled()) {
log.info("Create StandardInfo Size: " + list.size());
log.info("Letter Feedback Service Create End.");
}
return list;
}
private static final Log log = LogFactory.getLog(LetterFeedbackService.class);
// 领导信箱: model_id = 1
// pur_id与tbmail.type映射
public static final Map<Integer, Integer> PURID2TYPE_MAPPING = new HashMap<Integer, Integer>();
static {
PURID2TYPE_MAPPING.put(10, 347);
PURID2TYPE_MAPPING.put(12, 352);
PURID2TYPE_MAPPING.put(2, 375);
PURID2TYPE_MAPPING.put(4, 0);
}
@Override
public List<StandardInfo> createObjList() throws AhDataException {
if (log.isInfoEnabled()) {
log.info("Letter Feedback Service Create start...");
}
List<StandardInfo> list = new ArrayList<StandardInfo>();
List<XSqBean> sqList = XSqManager.extractSqList();
StandardInfo info = null;
Tbmail tbmail = null;
Integer tbmailType = null;
if (sqList != null && !sqList.isEmpty()) {
for (SQBean sq : sqList) {
if (log.isInfoEnabled()) {
log.info("sq_id: " + sq.getSq_id());
}
tbmailType = PURID2TYPE_MAPPING.get(sq.getPur_id());
tbmail = new Tbmail();
tbmail.setId(sq.getSq_id());
tbmail.setName(sq.getSq_realname());
tbmail.setMail(sq.getSq_email());
tbmail.setAdr(sq.getSq_address());
tbmail.setTel(sq.getSq_phone());
tbmail.setFax("");
tbmail.setOther("");
tbmail.setType(tbmailType != null ? tbmailType.intValue() : 0);
tbmail.setTitle(sq.getSq_title2());
tbmail.setContent(sq.getSq_content2());
tbmail.setHf("");
tbmail.setHf1("");
tbmail.setHfqk("");
try {
tbmail.setDatetime(DateUtil.getDate(sq.getSq_dtime()));
} catch (ParseException e) {
if (log.isErrorEnabled()) {
log.error(e.toString());
}
}
tbmail.setLb("");
info = new StandardInfo();
info.setObjectInfo(encodeTbmail(tbmail));
info.setTitle(tbmail.getTitle());
info.setBusinesstype("上海发改委来信反馈");
info.setDetail(tbmail.getContent());
info.setBusinessresult("");
list.add(info);
XSqManager.updateSqExtracted(sq.getSq_id());
}
}
if (log.isInfoEnabled()) {
log.info("Create StandardInfo Size: " + list.size());
log.info("Letter Feedback Service Create End.");
}
return list;
}
然后把项目部署之后就会生成wsdl
http://www.shdrc.gov.cn/services/LetterFeedback?wsdl
这就可以看到我们生成的配置文件
然后在另一边可以直接调用我们wsdl中的方法就ok
具体的实现类如下:
public class Main {
public static void main(String[] args) throws Exception {
Service serviceModel = new ObjectServiceFactory().create(IXzxkService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
// String serviceUrl =
// "http://192.168.137.4:1104/services/LetterFeedback";
//String serviceUrl = "http://www.shfgw.gov.cn/services/ConsProject";
String serviceUrl = "http://www.shdrc.gov.cn/services/LetterFeedback";
IXzxkService xzxkService = (IXzxkService) factory.create(serviceModel, serviceUrl);
/* StandardInfo info = new StandardInfo();
WebXmSjjh xmSjjh = new WebXmSjjh();
xmSjjh.setXmbm("aaaaaaaa");
xmSjjh.setXmmc("bbbbbb");
xmSjjh.setXmfr("cccc");
xmSjjh.setJsnr("dddd");
xmSjjh.setJsgm("中国一哥");
xmSjjh.setPfsj(new Date());
xmSjjh.setSpwh("是不是");
xmSjjh.setXmlb("项目建议书");
xmSjjh.setSpwj("yyyyy");
xmSjjh.setSqh("ab活期");
xmSjjh.setXmlx("7");
info.setObjectInfo(encodeWebXmSjjh(xmSjjh));
List<StandardInfo> list = new ArrayList<StandardInfo>();
list.add(info);
*/
// xzxkService.deliverObjList(list);
List<StandardInfo> list= xzxkService.createObjList();
System.out.println(list.size());
}
public static void main(String[] args) throws Exception {
Service serviceModel = new ObjectServiceFactory().create(IXzxkService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
// String serviceUrl =
// "http://192.168.137.4:1104/services/LetterFeedback";
//String serviceUrl = "http://www.shfgw.gov.cn/services/ConsProject";
String serviceUrl = "http://www.shdrc.gov.cn/services/LetterFeedback";
IXzxkService xzxkService = (IXzxkService) factory.create(serviceModel, serviceUrl);
/* StandardInfo info = new StandardInfo();
WebXmSjjh xmSjjh = new WebXmSjjh();
xmSjjh.setXmbm("aaaaaaaa");
xmSjjh.setXmmc("bbbbbb");
xmSjjh.setXmfr("cccc");
xmSjjh.setJsnr("dddd");
xmSjjh.setJsgm("中国一哥");
xmSjjh.setPfsj(new Date());
xmSjjh.setSpwh("是不是");
xmSjjh.setXmlb("项目建议书");
xmSjjh.setSpwj("yyyyy");
xmSjjh.setSqh("ab活期");
xmSjjh.setXmlx("7");
info.setObjectInfo(encodeWebXmSjjh(xmSjjh));
List<StandardInfo> list = new ArrayList<StandardInfo>();
list.add(info);
*/
// xzxkService.deliverObjList(list);
List<StandardInfo> list= xzxkService.createObjList();
System.out.println(list.size());
}