1.创建服务端
1.1创建公共ServletToBeanProxy
package com.mysql.sx.healthUploadInterface;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ServletToBeanProxy extends GenericServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private String targetBean;//当前客户端请求的Servlet名字
private Servlet proxy;//代理Servlet
@Override
public void init() throws ServletException {
super.init();
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); //初始化Spring容器
this.targetBean = getServletName();
this.proxy = (Servlet) wac.getBean(targetBean);//调用ServletBean
proxy.init(getServletConfig());//调用初始化方法将ServletConfig传给Bean
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
proxy.service(arg0, arg1);//在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post)
}
}
1.2 创建servlet类
package com.mysql.sx.healthUploadInterface.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.httpReq.ReturnMessage;
import com.common.util.CommonConst.SERVICE_CODE;
import com.common.util.WMessage;
import com.mysql.sx.healthUploadInterface.service.IEquipmentRegisterService;
import com.mysql.sx.healthUploadInterface.service.IInstrumentDataService;
import com.mysql.sx.healthUploadInterface.service.IMedicalTechniciansService;
import com.mysql.sx.healthUploadInterface.service.IOrganizationService;
import com.mysql.sx.healthUploadInterface.service.IPersonBasicsService;
import com.mysql.sx.healthUploadInterface.service.IUserECGService;
import com.mysql.sx.healthUploadInterface.service.IUploadResidentHealthRecordsService;
/**
* 健康一体机servlet类
*
* @author Administrator
*
*/
@Controller
@Scope("prototype")
public class HealthAllMachineServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(HealthAllMachineServlet.class);
public IPersonBasicsService personBasicsService; // 居民信息上传service
public IEquipmentRegisterService equipmentRegisterService; // 设备注册service
public IMedicalTechniciansService medicalTechniciansServiceImp; //上传医生数据
public IUserECGService userECGService;
public IUploadResidentHealthRecordsService uploadResidentHealthRecordsService;
public IOrganizationService organizationServiceImp; //获取设备名称
public IInstrumentDataService instrumentDataService;
@Override
public void init() throws ServletException {
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json; charset=utf-8");
resp.setCharacterEncoding("UTF-8");
String objjson = req.getParameter("json");
log.info("前端json:" + objjson);
// JSONParsingMethod jsonMethod = new JSONParsingMethod();
JSONArray jsonArray = this.getData(objjson);
log.info("返回json:" + jsonArray);
PrintWriter out = resp.getWriter();
out.write(jsonArray.toString());
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
public void destroy() {
// do nothing.
}
/**
* 解析客户获取的json数据
*
* @param objectJson
* 客户端传入的json数据
*/
public JSONArray getData(String objectJson) {
ReturnMessage retMessage = new ReturnMessage();
WMessage wm = new WMessage();
String serviceCode = "";
try {
// 将json字符串转换为json对象
JSONObject jsonObj = new JSONObject();
jsonObj = (JSONObject) JSONObject.parse(objectJson);
jsonObj.get("data");
log.info("date:" + jsonObj.get("data"));
serviceCode = jsonObj.get("SERVICE_CODE").toString();
if (serviceCode.equals(SERVICE_CODE.PERSONBASICS_SERVICE_CODE)) {
// 居民信息
wm = personBasicsService.insertPersion(jsonObj);
return retMessage.getRnMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus());
} else if (serviceCode.equals(SERVICE_CODE.PERSONBASICS_ARCHIVES_SERVICE_CODE)) {
// 居民健康档案
JSONArray list = jsonObj.getJSONArray("data");
wm = uploadResidentHealthRecordsService.addResidentHealthRecords(list);
} else if (serviceCode.equals(SERVICE_CODE.TECHNICIANS_SERVICE_CODE)) {
// 上传医生数据
wm = medicalTechniciansServiceImp.insertMedicalTechnicians(jsonObj);
} else if (serviceCode.equals(SERVICE_CODE.YH_UNION_SERVICE_CODE)) {
// 多参仪数据上传(血氧,血压,血糖,体温,尿常规,糖化血糖蛋白,快速检测,体质)
Map<String, Object> map = instrumentDataService.insertInsrtumentData(jsonObj);
wm=(WMessage) map.get("message");
@SuppressWarnings("unchecked")
Map<String, String> bodyMap = (Map<String, String>) map.get("bodyMap");
return retMessage.getInstrumentDataMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus(), bodyMap);
} else if (serviceCode.equals(SERVICE_CODE.YHXD_SERVICE_CODE)) {
// 上传心电文件
wm = userECGService.insertUserECG(jsonObj);
} else if (serviceCode.equals(SERVICE_CODE.ADMINDIVISION_SERVICE_CODE)) {
// 获取机构名称
Map<String ,Object> resultMap = organizationServiceImp.queryOrgNameByCode(jsonObj);
wm = (WMessage) resultMap.get("wm");
JSONObject jsonObject = (JSONObject) resultMap.get("jsonObject");
return retMessage.getQueryRnMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus(),jsonObject);
} else if (serviceCode.equals(SERVICE_CODE.EQUIPMENT_SERVICE_CODE)) {
// 设备注册
wm = equipmentRegisterService.insertEquipmentRegister(jsonObj);
return retMessage.getEquipmentRegisterMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus());
} else if (serviceCode.equals(SERVICE_CODE.MEDICAL_TECHNICIANS_SERVICE_CODE)) {
// 获取下载医生信息
JSONObject json= medicalTechniciansServiceImp.queryDoctor(jsonObj);
JSONObject body=json.getObject("BODY", JSONObject.class);
wm=json.getObject("WMessage", WMessage.class);
return retMessage.getQueryRnMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus(),body);
} else if (serviceCode.equals(SERVICE_CODE.mpi_personbasics_SERVICE_CODE)) {
// 下载更新居民信息
JSONObject jSONObject = personBasicsService.queryPersion(jsonObj);
wm=jSONObject.getObject("WMessage", WMessage.class);
JSONObject body = jSONObject.getObject("BODY",JSONObject.class);
return retMessage.getQueryRnMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus(),body);
}
log.info("serviceCode:" + serviceCode);
} catch (Exception e) {
wm.setRetStatus("F");
wm.setRetMsg(e.toString());
log.error("获取前端json数据错误:" + e);
}
return retMessage.getRnMessage(wm.getRetMsg(), serviceCode, wm.getRetStatus());
}
@Resource
public void setPersonBasicsService(IPersonBasicsService personBasicsService) {
this.personBasicsService = personBasicsService;
}
@Resource
public void setInstrumentDataService(IInstrumentDataService instrumentDataService) {
this.instrumentDataService = instrumentDataService;
}
@Resource
public void setOrganizationServiceImp(
IOrganizationService organizationServiceImp) {
this.organizationServiceImp = organizationServiceImp;
}
@Resource
public void setMedicalTechniciansServiceImp(IMedicalTechniciansService medicalTechniciansServiceImp) {
this.medicalTechniciansServiceImp = medicalTechniciansServiceImp;
}
@Resource
public void setEquipmentRegisterService(IEquipmentRegisterService equipmentRegisterService) {
this.equipmentRegisterService = equipmentRegisterService;
}
@Resource
public void setUploadResidentHealthRecordsService(IUploadResidentHealthRecordsService uploadResidentHealthRecordsService) {
this.uploadResidentHealthRecordsService = uploadResidentHealthRecordsService;
}
@Resource
public void setUserECGService(IUserECGService userECGService) {
this.userECGService = userECGService;
}
}
1.3 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 错误页 -->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
<!-- 配置servlet -->
<servlet>
<servlet-name>healthAllMachineServlet</servlet-name>
<servlet-class>com.mysql.sx.healthUploadInterface.ServletToBeanProxy</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>healthAllMachineServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
<!-- 拦截器防止乱码 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
1.4其他创建方法使用springmvc+mybatis
2.创建客户端调用服务端http://127.0.1:8080/serviceProxy/servlet
2.1创建common.properties文件
interfaceUrl = http://127.0.1:8080/serviceProxy/servlet
2.2.创建文件读取类Config.java
package com.jn.sys;
import java.util.Properties;
public class Config {
private static Properties properties;
public static void set(Properties p){
if(properties==null){
properties = p;
}
}
public static String get(String key){
return properties.getProperty(key);
}
}
2.3.创建接口调用类InterfaceMethod.java
package com.common.httpReq;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONObject;
import com.common.util.CommonConst.HTTP_ENTITY;
import com.common.util.CommonConst.RET_STATUS;
import com.common.util.CommonConst.USER_LOGIN_SERVICE;
import com.common.util.CommonConst.USER_MEDCORE_SERVICE;
import com.jn.common.CommonUtils;
import com.jn.common.LoginUserInfo;
import com.jn.sys.Config;
/**
* 接口公共方法
* @author Administrator
*
*/
public abstract class InterfaceMethod {
private static final Logger log = Logger.getLogger(InterfaceMethod.class);
/**
* 获取登陆用户所在机构
* @param orgCode 传登陆用户所在的机构代码
* @param token 用户登陆的token
* @return
*/
@SuppressWarnings({ "unchecked"})
public static String[] getMedCore(String orgCode,String token){
List<Map<String, Object>> lstObject = new ArrayList<Map<String, Object>>();
String[] strCode = null;
try{
Map<String, Object> jsonMap = new HashMap<String, Object>();
JSONObject object = new JSONObject();
object.put("SERVICE_CODE", USER_MEDCORE_SERVICE.SERVICE_CODE);
object.put("ORG_CODE", orgCode);
object.put("STATES", USER_MEDCORE_SERVICE.STATES);
object.put("TYPE", USER_MEDCORE_SERVICE.TYPE);
object.put("CONSUMER_ID", token);
String sr=HttpRequest.sendPost(Config.get("interfaceUrl"), "json="+object.toJSONString());
object = (JSONObject) JSONObject.parse(sr);
jsonMap =(Map<String, Object>)object.get(HTTP_ENTITY.SYS_HEAD);
if(jsonMap.get("RET_STATUS").equals(RET_STATUS.RET_STATUS_S)){
jsonMap =(Map<String, Object>)object.get(HTTP_ENTITY.BODY);
log.info("接口列表jsonMap:"+jsonMap);
lstObject = (List<Map<String, Object>>) jsonMap.get("data");
log.info("机构数:"+jsonMap.get("num").toString());
strCode= new String[Integer.parseInt(jsonMap.get("num").toString())+1];
int i = 0;
strCode[i] = orgCode;
if(lstObject.size()>0){
for(Map<String, Object> mapStr: lstObject){
strCode[i+1] = mapStr.get("ORG_CODE").toString();
log.info("机构编码strCode:"+strCode[i++]);
}
}
log.info("机构编码str:"+strCode);
}
}catch(Exception e){
log.error("获取登陆用户所在机构接口错误:"+e);
}
return strCode;
}
public static void main(String[] args){
String orgCode="固定参数";
String token="固定参数";
getMedCore(orgCode,token);
}
}
3.先启动服务端,在启动客户端,可用于测试。