访问路径统一分发服务

1.根据请求参数,进入相应接口

function postajax() {
	var url = "http://localhost:8080/server/api";
	var data = {
             	'cmd':101,
             	'type':1,
             	"name":'ai',
             	"startTime":'1711-01-23 00:00:00',
             	"endTime":'1900-10-11 00:00:00',
             	'page':1,
             	'pageSize':10
           };
	$.ajax({
        url : url,
        data : JSON.stringify(data),
        //async : asynct,
        timeout:30000,
		type: 'post',
		cache: false,
        dataType:'json',
        contentType:"application/json;charset=UTF-8",
        beforeSend:function(){
        },
        success : function(result,textStatus,jqXHR) {
        debugger
            if(jqXHR.status==200){
                //回调
                //result = eval('(' + result + ')');
                alert(result.data.msg);
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
           alert(XMLHttpRequest.statusText,2,null,null);
        },
        complete: function(XMLHttpRequest,status) {
            //超时,status还有success,error等值的情况
            if(status=='timeout'){
                alert("服务器响应超时,请稍后再试!",2,null,null);
            }
        },
    });
}

根据cmd进入相应的service,根据type进入相应的service方法

 

2.服务分发

/***
 * @deprecated 根據參數分發請求
 * @author sjwy-0001
 *
 */
@Slf4j
@RestController
@RequestMapping(value = "/server")
public class DispatcherController {
	
	@RequestMapping(value= "/api")
	@ResponseBody
	public ReturnResult dispatcher(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ServletInputStream inputStream = request.getInputStream();
        String pkt = IOUtils.toString(inputStream);
        log.info("請求參數為 》》》》》》》》》》》》》 :" + pkt);
        inputStream.close();
        if (pkt == null) {
            return ReturnResult.error("400", "請求參數不能為空;");
        }
        JSONObject reqParam = null; 
        try {
        	reqParam = JSON.parseObject(pkt); 
        	boolean checkParams = checkParams(reqParam);
        	if (!checkParams) {
        		return ReturnResult.error("400", "請求參數不合法;");
        	} 
        	Integer code = (Integer) reqParam.get("cmd");
        	String serviceName = getServiceNameByCmd(code);
        	if (StringUtils.isBlank(serviceName)) {
        		return ReturnResult.error("400", "code無法獲取指定的service;");
        	}
        	ICommonService ICommonService = (com.shy.springboot.service.ICommonService) InitializeBean.getBean(serviceName);
        	ReturnResult result = ICommonService.execute(reqParam);
        	return result;
        } catch (Exception e) {
			log.info("JSON 轉換異常 :" + e);
			e.printStackTrace();
			return ReturnResult.error("400", "數據服務異常;");
		}  

	}
	/***
	 * @deprecated 校驗請求參數是否合法
	 * @param params
	 * @return
	 */
	public boolean checkParams(JSONObject params) {
		boolean flag = true;
		try {
			Integer cmd = params.getInteger("cmd");
			Integer type = params.getInteger("type");
			if (cmd == null || type == null) {
				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("checkParams ex 校驗請求參數是否合法 :" + e);
			flag = false;
			return flag;
		}
		return flag;
	} 
	
	/***
	 * @deprecated 根據cmd獲取serviceBean
	 * @param cmd
	 * @return
	 */
	public String getServiceNameByCmd(int cmd) {
		String serviceName = "";
		switch (cmd) {
		case 101:
			serviceName = "userService";
			break;
		case 102:
			serviceName = "accountService";
			break;
		case 103:
			serviceName = "loggerService";
			break;
		default:
			break;
		}
		return serviceName;
	}
}

3.统一Service接口

public interface ICommonService {
	public ReturnResult execute(JSONObject json) throws Exception;
}

4.Service注入

import org.springframework.beans.BeansException;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;  
import org.springframework.stereotype.Service;  

@Service  
public class InitializeBean implements ApplicationContextAware {  
    private static ApplicationContext applicationContext;  
  
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
    	InitializeBean.applicationContext = applicationContext;  
    }  
  
    public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    }  
    public static Object getBean(String beanName) {  
        return applicationContext.getBean(beanName);  
    }  
      
    public static <T>T getBean(String beanName , Class<T>clazz) {  
        return applicationContext.getBean(beanName , clazz);  
    }  
}  

5.根据Type找到具体方法实现

@Slf4j
@Service
@Transactional
public class UserService implements ICommonService {

	@Resource
	private UserDao userDao;
	
	@Override
	public ReturnResult execute(JSONObject json) throws Exception {
		ReturnResult result = null;
		Integer type = (Integer) json.get("type");
		switch (type) {
        case 1:
        	result = getUserMapList(json);
            break;       
        case 2:
        	result = getUserInfo(json);
            break; 
        case 3:
        	result = addUser(json);
            break; 
        case 4:
        	result = updateUser(json);
            break; 
    }
		return result;
	}
	
	private ReturnResult getUserMapList(JSONObject json) throws Exception {
		String name = json.getString("name");
		String startTime = json.getString("startTime");
		String endTime = json.getString("endTime");
		Integer page = json.getInteger("page");
		Integer pageSize = json.getInteger("pageSize");
		Map<String, Object> userMapList = userDao.getUserMapList(name, startTime, endTime, page, pageSize);
		return ReturnResult.success(userMapList);
	}
	
	private ReturnResult getUserInfo(JSONObject json) throws Exception {
		//TODO
		return ReturnResult.success();
	}

	private ReturnResult addUser(JSONObject json) throws Exception {
		//TODO
		return ReturnResult.success();
	}
	
	private ReturnResult updateUser(JSONObject json) throws Exception {
		//TODO
		return ReturnResult.success();
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值