Browse Source

统一接口处理body体json参数

cuixubin 3 years ago
parent
commit
0cbb9f2db6
1 changed files with 36 additions and 0 deletions
  1. 36 0
      src/com/persagy/web/controller/entrance/EntranceController.java

+ 36 - 0
src/com/persagy/web/controller/entrance/EntranceController.java

@@ -3,6 +3,7 @@ package com.persagy.web.controller.entrance;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.net.URLDecoder;
+import java.util.Map;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -13,6 +14,7 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -74,6 +76,40 @@ public class EntranceController {
 	    return interfaceResult;
 	}
 	
+	@RequestMapping(value = "/unifierJson/{" + HANDLE_TYPE + "}")
+	@ResponseBody
+	public InterfaceResult unifierJson(@PathVariable(HANDLE_TYPE) String handleType,
+			@RequestBody Map<String, Object> param, HttpServletRequest request) throws Exception {
+
+		String ip = getIpAddr(request);
+		ConstBusinessStatic.add(ip, handleType);
+		RequestIpStatisticThread.addupdateIpApiEndDateMap(ip, handleType);
+		
+		Long start = System.currentTimeMillis();
+
+		BusinessService businessService = (BusinessService) context.getBean(handleType);
+		InterfaceResult interfaceResult = CommonUtils.createInterfaceResult();
+		
+		String jsonString = objectMapper.writeValueAsString(param);
+		try {
+			jsonString = URLDecoder.decode(jsonString, "UTF-8");
+			businessService.handle(jsonString, interfaceResult.getContent());
+			interfaceResult.setResult(Result.SUCCESS);
+		} catch (Exception e) {
+			interfaceResult.setResult(Result.FAILURE);
+			interfaceResult.setReason(e.getMessage());
+		    interfaceResult.getContent().clear();
+		    log.error("Request Error! requesterIp="+ip+" API=" + handleType + ", Param=" + jsonString + ", Msg=" + e.getMessage(), e);
+		}
+
+		long end = System.currentTimeMillis();
+	    long timeInterval = (end - start.longValue()) / 1000L;
+	    if (timeInterval > 3L) {
+	    	log.warn("TimeCost Warning! requesterIp=" +ip+ timeInterval + "s, API=" + handleType + ", Param=" + jsonString);
+	    }
+	    return interfaceResult;
+	}
+	
 	@RequestMapping(value = "/download/icon/{param}")
 	public void iconDownload(@PathVariable("param") String param, HttpServletRequest request, HttpServletResponse response) throws Exception {
 		String ip = getIpAddr(request);