package com.zichen.xhkq.controller.login;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.zichen.xhkq.exception.CustomException;
import com.zichen.xhkq.util.request.RequestAnalysisUtil;
@Controller
@RequestMapping("/testController")
public class TestController {
private static Logger log = LoggerFactory.getLogger(TestController.class);
/**
* 响应
*
* @param response
* @param outputStr
*/
private void doResponse(HttpServletResponse response, String outputStr) {
response.setContentType("application/json");
PrintWriter out = null;
try {
out = response.getWriter();
out.write(outputStr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
@RequestMapping("/testMy")
public void testMy(HttpServletRequest request, HttpServletResponse response)
throws CustomException, UnsupportedEncodingException {
// 1.入参解析打印
try {
JSONObject requestJsonObj = RequestAnalysisUtil
.parseRequest2JsonObject(request);
log.info("testMy|requestJsonObj={}", requestJsonObj);
String passWord = requestJsonObj.getString("passWord");// 交易信息
log.debug("passWord={}", passWord);
responseSuccess(response, requestJsonObj);
} catch (Exception e) {
responseField(response, e);
}
}
private void responseField(HttpServletResponse response, Exception e) {
log.error("testMy|系统异常e={}", e);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "-1");
jsonObject.put("result", e);
jsonObject.put("messages", "系统异常");
doResponse(response, jsonObject.toJSONString());
}
private void responseSuccess(HttpServletResponse response,
Object requestJsonObj) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "000000");
jsonObject.put("result", requestJsonObj);
jsonObject.put("messages", "成功了!");
doResponse(response, jsonObject.toJSONString());
}
}
package com.zichen.xhkq.util.request;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.zichen.xhkq.dict.xhPay.Dict_xhPay_encoding;
public class RequestAnalysisUtil {
/**
* parse Request 2 JsonObject
*
* @param jsonStr
* @return
* @throws Exception
*/
public static JSONObject parseRequest2JsonObject(HttpServletRequest request)
throws Exception {
JSONObject jsonObject = new JSONObject();
// 判断请求method,从request获取参数
String httpMehtod = request.getMethod();
// GET请求
if (httpMehtod.equals("GET")) {
String requestJsonString = new String(request.getQueryString()
.getBytes("UTF-8"), "utf-8").replaceAll("%22", "\"")
.replaceAll("%7B", "{").replaceAll("%7D", "}");
// 将get参数转为map,转为json
Map<String, String> notifyMap = parseString2Map(requestJsonString);
jsonObject = JSONObject.parseObject(JSONObject
.toJSONString(notifyMap));
} else if (httpMehtod.equals("POST")) {
String requestJsonString = parseHttpRequest(request,
Dict_xhPay_encoding.XHPAY_ENCODING_UTF8);
jsonObject = JSONObject.parseObject(requestJsonString);
} else {
throw new Exception("请求方法解析异常");
}
return jsonObject;
}
/**
* string parse 2 MapString
*
* @description 以&分割,key=value格式
* @author CNZZ
* @time 2019-1-5
* @param str
* @return
*/
public static Map<String, String> parseString2Map(String str) {
Map<String, String> map = new HashMap<String, String>();
String[] array = str.split("&");
for (int i = 0; i < array.length; i++) {
String[] data = array[i].split("=");
map.put(data[0], data[1]);
}
return map;
}
/**
* 读取请求题内容
*
* @param request
* @return
* @throws Exception
*/
public static String parseHttpRequest(HttpServletRequest request,
String encoding) throws Exception {
// 请求原文
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(
request.getInputStream(), encoding));
String str = null;
while ((str = br.readLine()) != null || " ".equals(str)) {
sb.append(str);
}
} catch (IOException e) {
throw new IOException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String resultStr = sb.toString();
return resultStr;
}
}