| New file |
| | |
| | | package com.zy.common.config; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.Cools; |
| | | import com.core.common.R; |
| | | import com.zy.asrs.service.ApiLogService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.MethodParameter; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.converter.HttpMessageConverter; |
| | | import org.springframework.http.server.ServerHttpRequest; |
| | | import org.springframework.http.server.ServerHttpResponse; |
| | | import org.springframework.http.server.ServletServerHttpRequest; |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | | import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * 返回值处理 |
| | | * Created by vincent on 2022/4/22 |
| | | */ |
| | | @Slf4j |
| | | @ControllerAdvice |
| | | public class ControllerResAdvice implements ResponseBodyAdvice<Object> { |
| | | |
| | | @Autowired |
| | | private ApiLogService apiLogService; |
| | | |
| | | @Override |
| | | public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { |
| | | if (serverHttpRequest instanceof ServletServerHttpRequest) { |
| | | HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest(); |
| | | Object appAuth = request.getAttribute("appAuth"); |
| | | if (appAuth != null) { |
| | | if (o instanceof R) { |
| | | String appkey = request.getHeader("appkey"); |
| | | Object reqCache = request.getAttribute("cache"); |
| | | if (!Cools.isEmpty(appkey)) { |
| | | // 保存接口日志 |
| | | apiLogService.save( |
| | | String.valueOf(appAuth), |
| | | request.getRequestURI(), |
| | | appkey, |
| | | "", |
| | | reqCache==null?"": JSON.toJSONString(reqCache), |
| | | JSON.toJSONString(o), |
| | | String.valueOf(((R) o).get("code")).equalsIgnoreCase("200") |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return o; |
| | | } |
| | | |
| | | public static String json(HttpServletRequest request) { |
| | | try { |
| | | BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8)); |
| | | StringBuilder sb = new StringBuilder(); |
| | | String inputStr; |
| | | while ((inputStr = streamReader.readLine()) != null) { |
| | | sb.append(inputStr); |
| | | } |
| | | return sb.toString(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | } |