https://blog.csdn.net/u012693016/article/details/94600158
1 拦截器
package com.jiutong.zqp.controll.web.interceptor; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class WebFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)servletRequest; req.setCharacterEncoding("UTF-8"); HttpServletResponse res = (HttpServletResponse)servletResponse; String originHeader=((HttpServletRequest)servletRequest).getHeader("Origin"); res.setHeader("Access-Control-Allow-Origin", originHeader); // res.setContentType("application/json;charset=UTF-8"); res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); res.setHeader("Access-Control-Max-Age", "3600"); res.setHeader("Access-Control-Allow-Headers", "Origin,No-Cache,X-Requested-With,If-Modified-Since,Pragma,Last-Modified,Cache-Control,Expires,Content-Type,X-E4M-With,userId,token,channelType");//表明服务器支持的所有头信息字段 res.setHeader("Access-Control-Allow-Credentials", "true"); //如果要把Cookie发到服务器,需要指定Access-Control-Allow-Credentials字段为true; res.setHeader("XDomainRequestAllowed","1"); filterChain.doFilter(req, res); return; } @Override public void destroy() { } }
xml文件配置
<filter> <filter-name>webFilter</filter-name> <filter-class>com.jiutong.zqp.controll.web.interceptor.WebFilter</filter-class> </filter>
2 RestTemplate
package com.jiutong.integration.ehr.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.google.common.collect.Lists; import com.jiutong.common.thirdpart.GuaZiMd5Util; import com.jiutong.constants.RedisConstans; import com.jiutong.integration.ehr.EhrClient; import com.jiutong.integration.ehr.bean.EhrBaseResponse; import com.jiutong.integration.ehr.bean.EhrMonthlyPerformanceBean; import com.jiutong.integration.ehr.dto.*; import com.jiutong.orderpay.enums.ServiceNameEnum; import com.jiutong.util.JedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.time.LocalDate; import java.util.List; import static com.jiutong.constants.RedisConstans.EHR_BDM_INFO; @Component @Slf4j public class EhrClientImpl implements EhrClient { @Value("${e***********************/}") private String ehrServiceUrl; @Value("${ehr.secretKey:610adac92bb}") private String secretKey = "610adac92bb"; @Autowired private RestTemplate restTemplate; private String getForEhrWithBdId(String url,Integer bdUserId) { String response = null; try { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Client-Id", ServiceNameEnum.E_APP.name()); String secret = GuaZiMd5Util.getMd5(secretKey+ServiceNameEnum.E_APP.name()+bdUserId); httpHeaders.add("Client-Secret", secret); ResponseEntity<String> responseEntity = restTemplate.exchange(ehrServiceUrl + url +"?hqpUserId=" + bdUserId, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class); response = responseEntity.getBody(); } catch (Exception e) { log.error("error call interface " + url + " e={}",e); } return response; } @Override public List<BdPerformanceDTO> getMonthlyPerformance(Integer bdUserId) { String response = getForEhrWithBdId("external/kpiMonthData", bdUserId); log.info("call interface getMonthlyPerformance bdUserId={},res={}",bdUserId,response); List<BdPerformanceDTO> bdPerformanceDTOS = Lists.newArrayList(); if (StringUtils.isBlank(response)) { log.error("error call interface getMonthlyPerformance fail"); return bdPerformanceDTOS; } EhrBaseResponse<List<EhrMonthlyPerformanceBean>> res = JSON.parseObject(response, new TypeReference<EhrBaseResponse<List<EhrMonthlyPerformanceBean>>>() { }); if (!"0000".equals(res.getCode())) { log.error("error call interface getMonthlyPerformance msg={}",res.getMsg()); return bdPerformanceDTOS; } if (CollectionUtils.isNotEmpty(res.getData())) { res.getData().forEach(e->{ BdPerformanceDTO bdPerformanceDTO = new BdPerformanceDTO(); bdPerformanceDTO.setKpiName(e.getKpi()); bdPerformanceDTO.setTarget(e.getObjective()); bdPerformanceDTO.setCompletion(e.getValue()); bdPerformanceDTO.setCompletionRate(e.getAccomplish()); bdPerformanceDTOS.add(bdPerformanceDTO); }); } return bdPerformanceDTOS; } @Override public UserLevelDTO getUserLevel(Integer bdUserId) { String response = getForEhrWithBdId("external/userLevel", bdUserId); log.info("call interface getUserLevel bdUserId={},res={}",bdUserId,response); if (StringUtils.isBlank(response)) { log.error("error call interface getUserLevel fail"); return null; } EhrBaseResponse<UserLevelDTO> res = JSON.parseObject(response, new TypeReference<EhrBaseResponse<UserLevelDTO>>() { }); if (!"0000".equals(res.getCode())) { log.error("error call interface getUserLevel msg={}",res.getMsg()); return null; } return res.getData(); } @Override public KpiAccomplishDTO getKpiAccomplish(Integer bdUserId) { String response = getForEhrWithBdId("external/kpiAccomplish", bdUserId); log.info("call interface getKpiAccomplish bdUserId={},res={}",bdUserId,response); if (StringUtils.isBlank(response)) { log.error("error call interface getKpiAccomplish fail"); return null; } EhrBaseResponse<KpiAccomplishDTO> res = JSON.parseObject(response, new TypeReference<EhrBaseResponse<KpiAccomplishDTO>>() { }); if (!"0000".equals(res.getCode())) { log.error("error call interface getKpiAccomplish msg={}",res.getMsg()); return null; } return res.getData(); } @Override public BdmInfoDTO getBdByBdm(Integer bdmUserId) { String key = RedisConstans.EHR_BDM_INFO + bdmUserId + ":" + LocalDate.now().toString(); BdmInfoDTO bdmInfoDTO = JedisUtil.getInstance().strings().get(key, BdmInfoDTO.class); if (bdmInfoDTO != null) { return bdmInfoDTO; } String response = getForEhrWithBdId("/external/employee/employeeSubordinate", bdmUserId); log.info("call interface getBdByBdm bdmUserId={},res={}",bdmUserId,response); if (StringUtils.isBlank(response)) { log.error("error call interface getEmployeeSubordinate fail"); return null; } EhrBaseResponse<BdmInfoDTO> res = JSON.parseObject(response, new TypeReference<EhrBaseResponse<BdmInfoDTO>>() { }); if (!"0000".equals(res.getCode())) { log.error("error call interface getBdByBdm msg={}",res.getMsg()); return null; } JedisUtil.getInstance().strings().setCover(key,res.getData(),60*60*24); return res.getData(); } @Override public ManagementCityDto getManagementCityByUserId(Integer userId) { String key = RedisConstans.EHR_USER_ROLE + userId + ":" + LocalDate.now().toString(); ManagementCityDto managementCityDto = JedisUtil.getInstance().strings().get(key, ManagementCityDto.class); if (managementCityDto != null) { return managementCityDto; } String response = getForEhrWithBdId("/external/employee/managementCity", userId); log.info("call interface getManagementCityByUserId userId={},res={}",userId,response); if (StringUtils.isBlank(response)) { log.error("error call interface getManagementCityByUserId fail"); return null; } EhrBaseResponse<ManagementCityDto> res = JSON.parseObject(response, new TypeReference<EhrBaseResponse<ManagementCityDto>>() { }); if (!"0000".equals(res.getCode())) { log.error("error call interface getManagementCityByUserId msg={}",res.getMsg()); return null; } JedisUtil.getInstance().strings().setCover(key,res.getData(),60*60*24); return res.getData(); } }