You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							162 lines
						
					
					
						
							5.0 KiB
						
					
					
				
			
		
		
	
	
							162 lines
						
					
					
						
							5.0 KiB
						
					
					
				| package com.ynxbd.wx.servlet.base;
 | |
| 
 | |
| import com.ynxbd.common.helper.common.HttpHelper;
 | |
| import com.ynxbd.common.helper.common.JsonHelper;
 | |
| import com.ynxbd.common.result.Result;
 | |
| import lombok.extern.slf4j.Slf4j;
 | |
| import org.slf4j.MDC;
 | |
| 
 | |
| import javax.servlet.http.HttpServlet;
 | |
| import javax.servlet.http.HttpServletRequest;
 | |
| import javax.servlet.http.HttpServletResponse;
 | |
| import java.io.IOException;
 | |
| import java.io.PrintWriter;
 | |
| import java.util.List;
 | |
| import java.util.Map;
 | |
| 
 | |
| // servlet基类
 | |
| @Slf4j
 | |
| public abstract class BaseServlet extends HttpServlet {
 | |
| 
 | |
|     @Override
 | |
|     public void init() {
 | |
|     }
 | |
| 
 | |
|     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 | |
|         MDC.remove("ip");
 | |
|         MDC.put("ip", HttpHelper.getIpAddress(req));
 | |
|         doPost(req, resp);
 | |
|     }
 | |
| 
 | |
|     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 | |
|         req.setCharacterEncoding("utf-8");
 | |
|         resp.setCharacterEncoding("utf-8");
 | |
| 
 | |
|         try {
 | |
|             // 响应数据处理
 | |
|             Result result = requestMapping(req, resp);
 | |
|             outResp(resp, result);
 | |
|         } catch (Exception e) {
 | |
|             e.printStackTrace();
 | |
|             outResp(resp, Result.error(e.getMessage()));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 处理请求内容
 | |
|      *
 | |
|      * @param req  请求
 | |
|      * @param resp 响应
 | |
|      * @return 响应内容
 | |
|      */
 | |
|     protected abstract Result requestMapping(HttpServletRequest req, HttpServletResponse resp) throws Exception;
 | |
| 
 | |
| 
 | |
|     /**
 | |
|      * 是否打印请求参数
 | |
|      *
 | |
|      * @param request 请求对象
 | |
|      */
 | |
|     protected static void logParams(HttpServletRequest request) {
 | |
|         Map<String, String[]> parameterMap = request.getParameterMap();
 | |
|         String p;
 | |
|         int cur = 0;
 | |
|         int size = parameterMap.size();
 | |
| 
 | |
|         StringBuilder params = new StringBuilder();
 | |
|         for (Map.Entry<String, String[]> map : parameterMap.entrySet()) {
 | |
|             cur++;
 | |
|             String val = map.getValue()[0];
 | |
|             if (val.length() > 666) {
 | |
|                 val = "参数过长,不进行打印";
 | |
|             }
 | |
|             p = map.getKey() + "=" + val;
 | |
|             if (cur != size) {
 | |
|                 p += ", ";
 | |
|             }
 | |
|             params.append(p);
 | |
|         }
 | |
| 
 | |
|         log.info(request.getServletPath() + "[请求参数:{{}}]", params);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 设置响应内容
 | |
|      *
 | |
|      * @param data 数据
 | |
|      * @param resp 响应流
 | |
|      */
 | |
|     private static void outResp(HttpServletResponse resp, Object data) {
 | |
|         resp.setContentType("application/json;charset=utf-8");
 | |
|         /* 允许跨域的主机地址 */
 | |
|         resp.setHeader("Access-Control-Allow-Origin", "*");
 | |
|         /* 允许跨域的请求方法GET, POST, HEAD 等 */
 | |
|         resp.setHeader("Access-Control-Allow-Methods", "*");
 | |
|         /* 重新预检验跨域的缓存时间 (s) */
 | |
|         resp.setHeader("Access-Control-Max-Age", "3600");
 | |
|         /* 允许跨域的请求头 */
 | |
|         resp.setHeader("Access-Control-Allow-Headers", "*");
 | |
|         /* 是否携带cookie */
 | |
|         resp.setHeader("Access-Control-Allow-Credentials", "true");
 | |
| 
 | |
|         try (PrintWriter pw = resp.getWriter()) {
 | |
|             pw.write(JsonHelper.toJsonString(data));
 | |
|             pw.flush();
 | |
|         } catch (IOException e) {
 | |
|             e.printStackTrace();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取对象
 | |
|      *
 | |
|      * @param req   请求
 | |
|      * @param key   对象key
 | |
|      * @param clazz 类
 | |
|      * @param <T>   T
 | |
|      * @return 对象
 | |
|      */
 | |
|     protected static <T> T getMapObject(HttpServletRequest req, String key, Class<T> clazz) {
 | |
|         Map<String, String[]> map = req.getParameterMap();
 | |
|         T t = null;
 | |
|         for (Map.Entry<String, String[]> m : map.entrySet()) {
 | |
|             if (m.getKey().equals(key)) {
 | |
|                 String val = m.getValue()[0];
 | |
|                 if (val == null) return null;
 | |
|                 try {
 | |
|                     t = JsonHelper.parseObject(val, clazz);
 | |
|                 } catch (Exception e) {
 | |
|                     return null;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         return t;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 获取对象
 | |
|      *
 | |
|      * @param req   请求
 | |
|      * @param key   对象key
 | |
|      * @param clazz 类
 | |
|      * @param <T>   T
 | |
|      * @return 对象
 | |
|      */
 | |
|     protected static <T> List<T> getMapList(HttpServletRequest req, String key, Class<T> clazz) {
 | |
|         Map<String, String[]> map = req.getParameterMap();
 | |
|         List<T> ts = null;
 | |
|         for (Map.Entry<String, String[]> m : map.entrySet()) {
 | |
|             if (m.getKey().equals(key)) {
 | |
|                 String val = m.getValue()[0];
 | |
|                 if (val == null) return null;
 | |
|                 try {
 | |
|                     ts = JsonHelper.parseArray(val, clazz);
 | |
|                 } catch (Exception e) {
 | |
|                     return null;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         if (ts == null) return null;
 | |
|         return ts.size() > 0 ? ts : null;
 | |
|     }
 | |
| }
 | |
| 
 |