package com.lesso.common.security;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lesso.common.enumeration.DeleteFlag;
import com.lesso.common.network.Response;
import com.lesso.pojo.language.LangContrast;
import com.lesso.service.language.LangContrastService;
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.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.lesso.common.network.Response.Meta;
import com.lesso.control.BaseController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 我的业务逻辑需要这个公共类继承了它;根据个人情况写
@ControllerAdvice
public class ResponseControllerAdviceUtil extends BaseController implements ResponseBodyAdvice<Object> {
@Autowired
LangContrastService langContrastService;
// private String appClient;
@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) {
// 以下处理返回的结果,根据自己需要改
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("X-Token");
if (token == null) {
return o;
}
Response re = (Response)o;
Meta meta = re.getMeta();
if(meta !=null && meta.getMessage() !=null && !getLangKey(request).equals("zh_lang")){
Map<String,Object> map = new HashMap<>();
// map.put("langKey", getLangKey(request));
map.put("langKey", "en_lang");
map.put("keyword", meta.getMessage().trim());
map.put("byOne",1 );
map.put("delflag", DeleteFlag.VALID.getCode());
List<LangContrast> list = langContrastService.getLangKeyByName(map);
if(list !=null && list.size()>0) {
System.out.println("hhhh:"+list.get(0).getLang());
meta.setMessage(list.get(0).getLang());
return re;
}
}
return o;
}
}
处理response拦截结果集
最新推荐文章于 2022-04-19 16:35:11 发布