天天看點

spring+xfire開發webservice

服務端需要的jar

spring+xfire開發webservice

接口實作類

package com.hongwei.futures.web.ws.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ServiceException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSErrorCode;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.CannedAccessControlList;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.cloopen.rest.sdk.utils.encoder.BASE64Decoder;
import com.hongwei.futures.model.FuBankCard;
import com.hongwei.futures.model.FuSmsLog;
import com.hongwei.futures.model.FuUser;
import com.hongwei.futures.model.HhrLogin;
import com.hongwei.futures.model.HhrQualiRegister;
import com.hongwei.futures.model.HhrStat;
import com.hongwei.futures.service.FuBankCardService;
import com.hongwei.futures.service.FuSmsLogService;
import com.hongwei.futures.service.FuUserService;
import com.hongwei.futures.service.HhrLoginService;
import com.hongwei.futures.service.HhrQualiRegisterService;
import com.hongwei.futures.service.HhrStatService;
import com.hongwei.futures.util.CommonUtil;
import com.hongwei.futures.util.IP4;
import com.hongwei.futures.web.ws.AppWebService;

public class AppWebServiceImpl implements AppWebService {

	private String[] types = new String[] { ".bmp", ".png", ".gif", ".jpeg", ".pjpeg", ".jpg" };
	private static final String ACCESS_ID = "VyeVZti9TY3t2NOU";
	private static final String ACCESS_KEY = "lR05tqefMFeEShbPvg7CbsAxtBGPka";

	@Resource
	private FuUserService fuUserService;
	@Resource
	private FuBankCardService fuBankCardService;
	@Resource
	private HhrLoginService hhrLoginService;
	@Resource
	private HhrStatService hhrStatService;
	@Resource
	private FuSmsLogService fuSmsLogService;
	@Resource
	private HhrQualiRegisterService hhrQualiRegisterService;

	
	/**
	 * 登陸
	 */
	public String login(String phone, String password) {
		JSONObject obj = new JSONObject();
		try {
			if (ServletActionContext.getRequest().getMethod().equals("POST")) {
				if (phone != null && password != null) {
					FuUser fuUser = fuUserService.findUserByRegPhone(phone);
					if (null != fuUser) {
						if (fuUser.getPassword().equals(password)) {
							obj.put("user_id", fuUser.getId());
							obj.put("invitation_code",
									fuUser.getInvitationCode() == null ? ""
											: fuUser.getInvitationCode());
							obj.put("is_success", 1);
							obj.put("message", "登陸成功");
							HhrLogin login = new HhrLogin();
							login.setFuUser(fuUser);
							login.setIp(IP4.getIP4(ServletActionContext
									.getRequest()));
							login.setLoginTime(new Date());
							hhrLoginService.save(login);
						} else {
							obj.put("message", "密碼錯誤");
							obj.put("is_success", 0);
						}
					} else {
						obj.put("message", "該手機号尚未注冊");
						obj.put("is_success", 0);
					}
				} else {
					obj.put("message", "缺少參數");
					obj.put("is_success", 0);
				}
			} else {
				obj.put("message", "非法請求");
				obj.put("is_success", 0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.out.println(obj);
		return obj.toString();
	}

	
	/**
	 * 主界面
	 */
	public String index(Long user_id, String invitation_code, String phone) {
		JSONObject obj = new JSONObject();
		try {
			if (ServletActionContext.getRequest().getMethod().equals("POST")) {
				if (user_id != null && invitation_code != null && phone != null) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("user_id", user_id);
					//map.put("invitation_code", invitation_code);
					//map.put("phone", phone);
					//map.put("stat_date", sdf.format(new Date()));
					Object[] user = fuUserService.findUserDataByMap(map);
					if (user != null) {
						Map<String, Object> map1 = new HashMap<String, Object>();
						map1.put("id", user[0]);
						map1.put("account_name", user[1]);
						map1.put("user_avatar", user[2]);
						map1.put("phone", user[3]);
						obj.put("user", map1);

						Object[] stat = hhrStatService.findStatDataByMap(map);
						Map<String, Object> map2 = new HashMap<String, Object>();
						map2.put("hhr_level", stat[0] != null ? stat[0]:"0");
						map2.put("firstly_partner_num", stat[1] != null ? stat[1]:"0");
						map2.put("secondary_partner_num", stat[2] != null ? stat[2]:"0");
						map2.put("extend_person_new", stat[3] != null ? stat[3]:"0");
						map2.put("interest_return_coefficient", stat[4] != null ? stat[4]:"0");
						map2.put("charges_return_coefficient", stat[5] != null ? stat[5]:"0");
						map2.put("daily_income", stat[6] != null ? stat[6]:"0");
						map2.put("monthly_income", stat[7] != null ? stat[7]:"0");
						obj.put("stat", map2);
						obj.put("messge", "成功");
						obj.put("is_success", 1);
					} else {
						obj.put("messge", "沒有此使用者!");
						obj.put("is_success", 0);
					}
				} else {
					obj.put("messge", "缺少參數");
					obj.put("is_success", 0);
				}
			} else {
				obj.put("messge", "invalid request");
				obj.put("is_success", 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(obj);
		return obj.toString();
	}
	
}
           

spring配置

<!-- xfire webservice整合spring接口配置 -->
	<!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/> web.xml如果引入了檔案這裡就不需要配置-->
	<bean id="webServiceImpl" class="com.hongwei.futures.web.ws.impl.AppWebServiceImpl"/>
	<bean name="queryServcie" class="org.codehaus.xfire.spring.ServiceBean">
		<property name="serviceBean" ref="webServiceImpl"/>
		<property name="serviceClass" value="com.hongwei.futures.web.ws.AppWebService"/>
		<property name="inHandlers">
			<list>
				<ref bean="addressingHandler" />
			</list>
		</property>
	</bean>
	<!--這個不需要改變,定義的是Handler -->
	<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
           

web.xml配置

<servlet>
		<servlet-name>XFireServlet</servlet-name>
		<!-- 不整合時使用org.codehaus.xfire.transport.http.XFireConfigurableServlet -->
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
           

在目錄WEB-INF/META-INF下建個xfire/service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<service xmlns="http://xfire.codehaus.org/config/1.0">
		<name>AppWebService</name>
		<serviceClass>com.hongwei.futures.web.ws.AppWebService</serviceClass>
		<implementationClass>com.hongwei.futures.web.ws.impl.AppWebServiceImpl</implementationClass>

               <!--多個webservice-->
		<name>TradeWebService</name>
		<serviceClass>com.hongwei.futures.web.trade.TradeWebService</serviceClass>
		<implementationClass>com.hongwei.futures.web.trade.impl.TradeWebServiceImpl</implementationClass>
	</service>
</beans>
           

下面是測試類

package com.hongwei.futures.util;

import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;

import com.cloopen.rest.sdk.utils.encoder.BASE64Encoder;

public class TestWebService {
	/**
	 * 登陸
	 */
	public void login() {
		String wsdl = "http://localhost:8080/services/AppWebService?wsdl";
		try {
			Client client = new Client(new URL(wsdl));
			System.out.println("開始調用");
			Object[] result = client.invoke("login", new Object[] {"15271943588", "e10adc3949ba59abbe56e057f20f883e"});
			for (Object o : result) {
				System.out.println(o);
			}
			System.out.println("結束調用");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 主界面
	 */
	public void index() {
		String wsdl = "http://localhost:8080/services/AppWebService?wsdl";
		try {
			Client client = new Client(new URL(wsdl));
			System.out.println("開始調用");
			Object[] result = client.invoke("index", new Object[] {257, "967542", "15271943580"});
			for (Object o : result) {
				System.out.println(o);
			}
			System.out.println("結束調用");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestWebService client = new TestWebService();
		client.login();
	}
}
           

繼續閱讀