本文主要是介绍xFire 实现webservice,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现webservice所需的jar包
服务器端实现:
业务接口实现:
package com.sk.core.webservice.server;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface QzqdServer {
public String saveQzqd(String message);
public String updateQzqd(String message);
public String deleteQzqd(String message);
}
业务实现类:
package com.sk.core.webservice.server.impl;
import java.util.HashMap;
import java.util.Iterator;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.xfire.transport.http.XFireServletController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sk.cms.entity.main.CmsSite;
import com.sk.cms.entity.main.CmsUser;
import com.sk.cms.entity.main.Content;
import com.sk.cms.entity.main.ContentExt;
import com.sk.cms.entity.main.ContentTxt;
import com.sk.cms.entity.main.ContentType;
import com.sk.cms.manager.main.CmsSiteMng;
import com.sk.cms.manager.main.CmsUserMng;
import com.sk.cms.manager.main.ContentMng;
import com.sk.cms.web.CmsUtils;
import com.sk.cms.web.WebErrors;
import com.sk.core.manager.UnifiedUserMng;
import com.sk.core.webservice.server.QzqdServer;
@Service
public class QzqdServerImpl implements QzqdServer{
private static Log log=LogFactory.getLog(QzqdServerImpl.class);
@Autowired
private ContentMng contentMng;
@Autowired
private CmsUserMng cmsUserMng;
@Autowired
private UnifiedUserMng unifiedUserMng;
@Autowired
private CmsSiteMng cmsSiteMng;
/**
* json 数据格式{"title":"","channelId":"","typeId":"","attr":{
* "codeId":"","code":"","xzzqsx":"","xzzqmc":"",.......
* },"username":"","password":""}
*/
public String saveQzqd(String message) {
HashMap<String, String> messMap = new HashMap<String, String>();
try{
//HttpServletRequest request = XFireServletController.getRequest();
CmsSite site = cmsSiteMng.findById(1);
JSONObject json =new JSONObject(message);
String title=json.getString("title");
String channelId=json.getString("channelId");
String username=json.getString("username");
String password=json.getString("password");
String picPath=null;
String picDesc=null;
if(json.has("picPath")){
picPath=json.getString("picPath");
}
if(json.has("picDesc")){
picDesc=json.getString("picDesc");
}
//String typeId=json.getString("typeId");
HashMap<String,String> attr=toHashMap(json.getString("attr"));
Content bean=new Content();
ContentExt ext=new ContentExt();
ContentTxt txt=new ContentTxt();
Integer[] channelIds = null;
Integer[] topicIds = null;
Integer typeId=1;
Integer[] viewGroupIds = null;
String[] attachmentPaths = null;
String[] attachmentNames =null;
String[] attachmentFilenames = null;
String[] picPaths={picPath};
String[] picDescs = {picDesc};
String[] tagArr = null;
Boolean draft = null;
CmsUser user = cmsUserMng.findById(unifiedUserMng.getByUsernameAndPass(username, password).getId());// 获取管理员用户
if(null!=user&&!user.getDisabled()){
bean.setUser(user);
bean.setAttr(attr);
bean.setSite(site);
ext.setTitle(title);
bean=contentMng.save(bean, ext, txt, channelIds, topicIds, viewGroupIds,
tagArr, attachmentPaths, attachmentNames, attachmentFilenames,
picPaths, picDescs, Integer.valueOf(channelId), typeId, draft, user, false);
messMap.put("status", "1");
messMap.put("contentId",String.valueOf(bean.getId()));
}else{
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
}
}catch(org.json.JSONException e){
messMap.put("status", "0");
messMap.put("message", "必需参数存在空值");
log.info(e.getMessage());
}
catch(java.lang.NullPointerException e){
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
log.info(e.getMessage());
}
catch(Exception e){
messMap.put("status", "0");
messMap.put("message", "数据保存异常");
log.info(e.getMessage());
}
JSONObject object=new JSONObject(messMap);
return object.toString();
}
public String updateQzqd(String message) {
HashMap<String, String> messMap = new HashMap<String, String>();
try{
//HttpServletRequest request = XFireServletController.getRequest();
CmsSite site = cmsSiteMng.findById(1);
JSONObject json =new JSONObject(message);
String title=null;
if(json.has("title")){
title=json.getString("title");
}
String picPath=null;
String picDesc=null;
if(json.has("picPath")){
picPath=json.getString("picPath");
}
if(json.has("picDesc")){
picDesc=json.getString("picDesc");
}
String cid=null;
Integer channelId=null;
if(json.has("channelId")){
cid=json.getString("channelId");
if(cid!=null&&!"".equals("")){
channelId=Integer.valueOf(cid);
}
}
String username=json.getString("username");
String password=json.getString("password");
String id=json.getString("id");
HashMap<String,String> attr=null;
if(json.has("attr")){
attr=toHashMap(json.getString("attr"));
}
Content bean=new Content();
ContentExt ext=new ContentExt();
ContentTxt txt=new ContentTxt();
Integer[] channelIds = null;
Integer[] topicIds = null;
Integer typeId=1;
Integer[] viewGroupIds = null;
String[] attachmentPaths = null;
String[] attachmentNames =null;
String[] attachmentFilenames = null;
String[] picPaths = {picPath};
String[] picDescs = {picDesc};
String[] tagArr = null;
Boolean draft = null;
CmsUser user = cmsUserMng.findById(unifiedUserMng.getByUsernameAndPass(username, password).getId());// 获取管理员用户
if(null!=user&&!user.getDisabled()){
bean.setId(Integer.valueOf(id));
bean.setUser(user);
//bean.setAttr(attr);
bean.setSite(site);
if(title!=null&&!"".equals(title)){
ext.setTitle(title);
}
ext.setId(Integer.valueOf(id));
bean=contentMng.update(bean, ext, txt, tagArr, channelIds, topicIds, viewGroupIds, attachmentPaths, attachmentNames, attachmentFilenames, picPaths, picDescs, attr, channelId, typeId, draft, user, false);
messMap.put("status", "1");
messMap.put("contentId",String.valueOf(bean.getId()));
}else{
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
}
}catch(org.json.JSONException e){
messMap.put("status", "0");
messMap.put("message", "必需参数存在空值");
log.info(e.getMessage());
}
catch(java.lang.NullPointerException e){
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
log.info(e.getMessage());
}
catch(Exception e){
messMap.put("status", "0");
messMap.put("message", "数据跟新异常");
log.info(e.getMessage());
e.printStackTrace();
}
JSONObject object=new JSONObject(messMap);
return object.toString();
}
public String deleteQzqd(String message) {
HashMap<String, String> messMap = new HashMap<String, String>();
try{
JSONObject json =new JSONObject(message);
String id=json.getString("id");
String username=json.getString("username");
String password=json.getString("password");
CmsUser user = cmsUserMng.findById(unifiedUserMng.getByUsernameAndPass(username, password).getId());// 获取管理员用户
if(null!=user&&!user.getDisabled()){
Content bean=contentMng.deleteById(Integer.valueOf(id));
messMap.put("status", "1");
messMap.put("contentId",String.valueOf(bean.getId()));
}else{
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
}
}catch(org.json.JSONException e){
messMap.put("status", "0");
messMap.put("message", "必需参数存在空值");
log.info(e.getMessage());
}
catch(java.lang.NullPointerException e){
messMap.put("status", "0");
messMap.put("message", "用户名或密码错误");
log.info(e.getMessage());
}
catch(Exception e){
messMap.put("status", "0");
messMap.put("message", "数据保存异常");
log.info(e.getMessage());
}
JSONObject object=new JSONObject(messMap);
return object.toString();
}
/**
* 将json格式的字符串解析成Map对象 <li>
* json格式:{"name":"admin","retries":"3fff","testname"
* :"ddd","testretries":"fffffffff"}
* @throws JSONException
*/
private HashMap<String, String> toHashMap(String object) throws JSONException
{
HashMap<String, String> data = new HashMap<String, String>();
// 将json字符串转换成jsonObject
JSONObject jsonObject = new JSONObject(object);
Iterator it = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (it.hasNext())
{
String key = String.valueOf(it.next());
String value = jsonObject.getString(key);
data.put(key, value);
}
return data;
}
}
在web.xml文件中添加:
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- <servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>-->
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在这个URI下开放Web Service服务 -->
<url-pattern>/service/XFireServlet/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
xfire-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="webServiceClient" class="com.sk.core.webservice.client.WebServiceClient"/>
<bean id="HelloWorldBean" class="com.sk.core.webservice.impl.HelloServerImpl"/>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/service/XFireServlet/HelloWorldService.ws">
<ref bean="HelloWorldService" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="HelloWorldService" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="HelloWorldBean" />
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="com.sk.core.webservice.HelloServer" />
</bean>
<!-- 以下是lt在2016-03-21添加权责清单webservice服务 -->
<bean id="qzqdService" class="com.sk.core.webservice.server.impl.QzqdServerImpl"/>
<bean id="qzqdWebService" parent="baseWebService">
<property name="serviceBean" ref="qzqdService"/>
<property name="serviceClass" value="com.sk.core.webservice.server.QzqdServer"/>
</bean>
</beans>
客户端测试类:
package sktask;
import java.net.URL;
import java.util.Date;
import org.apache.commons.httpclient.util.HttpURLConnection;
import org.codehaus.xfire.client.Client;
public class WebServiceTest {
public boolean newMessage() {
Client client;
try {
client = new Client(
new URL(
"http://localhost:9090/service/XFireServlet/QzqdServer?wsdl"));
// 根据WSDL创建客户实例
String[] objArray = new String[1];
objArray[0] = "{'id':'14315','picPath':'http://dpxq.gov.cn/u/cms/www/201603/011705420vfw.jpg','title':'test修改222','username':'webqzqd','password':'e10adc3949ba59abbe56e057f20f883e'}";
//objArray[0] = "{'picPath':'http://dpxq.gov.cn/u/cms/www/201603/011705420vfw.jpg','title':'test2','channelId':'786','username':'webqzqd','password':'e10adc3949ba59abbe56e057f20f883e'}";
//objArray[0] = "{'id':'14314','username':'webqzqd','password':'e10adc3949ba59abbe56e057f20f883e'}";
// 调用特定的Web Service方法
//Object[] results = client.invoke("saveQzqd", objArray);
Object[] results = client.invoke("updateQzqd", objArray);
//Object[] results = client.invoke("deleteQzqd", objArray);
System.out.println("newMessage result: " + results[0]);
return Boolean.parseBoolean(results[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args){
WebServiceTest test=new WebServiceTest();
test.newMessage();
}
}
这篇关于xFire 实现webservice的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!