ResFilterAspect.java
/**
* @author yf
* @version 1.0 2023/6/29
*/
@Aspect
@Component
@Slf4j
public class ResFilterAspect {
@Pointcut("@annotation(com.aop.annotation.ResFilter)")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point){
long beginTime = System.currentTimeMillis();
Object data = null;
try {
// 处理方法返回结果
data = point.proceed();
if (data == null){
return null;
}
if (data instanceof String){
data = ResFilterUtil.filter((String) data);
}
if (data instanceof RetData){
RetData retData;
retData = (RetData) data;
String s = ResFilterUtil.filter(JSON.toJSONString(retData.getExtra()));
retData.setExtra(JSONObject.parse(s));
data = retData;
}
} catch (Throwable e) {
log.error("response data filter fail:",e);
}
long time = System.currentTimeMillis() - beginTime;
log.info("response data filter success,use time:{}",time );
return data;
}
}
ResFilter注解
/**
* @author yf
* @version 1.0 2023/6/29
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResFilter {
String value() default "b2b_response_data_filter";
}
ResFilterUtil .java
/**
* response返回数据过滤
* @author yf
* @version 1.0 2023/6/19
*/
@Slf4j
public class ResFilterUtil {
private static final List<String> filterList = Lists.newArrayList("fareType","uniteFlightRebate","frDetailPriceMarkup","supplierMarkup","bookingIncentive", "frBasicPriceMarkup", "ticketSupplierCode", "rebateType", "policyRebate");
private static final List<String> ignoreFilterList = Lists.newArrayList( "policyRebate","ticketSupplierCode");
private static final String properties = "/unameDisplaySupplierCode.properties";
private static final String displaySupplierCodeList = "displaySupplierCodeList";
public static void main(String[] args) {
//System.out.println(filter("{\"allChinaLine\":false,\"gdsType\":\"WORLDSPAN\",\"gds\":\"1S\",\"airportNameMap\":{\"BKK\":{\"airportChineseName\":\"素万那普机场\",\"airportCode\":\"BKK\",\"airportEnglishName\":\"BANGKOK INTERNATIONAL AIRPORT\",\"airportShortSpell\":\"swnpjc\",\"airportSpell\":\"suwannapujichang\",\"cityChinese\":\"曼谷\",\"cityCode\":\"BKK\",\"cityEnglish\":\"BANGKOK\",\"countryChinese\":\"泰国\",\"countryEnglish\":\"THAILAND\",\"provinceChinese\":\"\",\"provinceCode\":\"\",\"provinceEnglish\":\"\"},\"HKG\":{\"airportChineseName\":\"香港国际机场\",\"airportCode\":\"HKG\",\"airportEnglishName\":\"HONG KONG INTERNATIONAL AIRPORT\",\"airportShortSpell\":\"xggjjc\",\"airportSpell\":\"xianggangguojijichang\",\"cityChinese\":\"香港\",\"cityCode\":\"HKG\",\"cityEnglish\":\"HONG KONG\",\"countryChinese\":\"中国香港\",\"countryEnglish\":\"HONG KONG\",\"provinceChinese\":\"中国香港\",\"provinceCode\":\"HK\",\"provinceEnglish\":\"HONG KONG\"}},\"adultNum\":1,\"childNum\":0,\"maxSeats\":8,\"flightType\":\"1\",\"supplierCode\":\"HK1\",\"usOrCaRoute\":\"false\",\"multiPriceConf\":{\"noSeatLowest\":[],\"preferentialTicket\":[],\"preferredService\":[],\"quickTicket\":[{\"carrier\":\"UO\",\"carrierIncludeType\":1,\"code\":\"ticketing_speed\",\"limitTime\":10,\"timeUnit\":\"m\",\"type\":1}],\"refundRule\":[{\"carrier\":\"\",\"carrierIncludeType\":0,\"code\":\"standard_refund_reschedule\",\"type\":2}],\"specialPolicy\":[],\"vouchersRefund\":[],\"workTime\":[]},\"supportKorea\":false,\"rescheduleTimeDto\":{\"saturdayDto\":{},\"sundayDto\":{},\"workDayDto\":{\"sanmeWorkday\":{\"beginHour\":9,\"beginMinute\":0,\"endHour\":17,\"endMinute\":30}}},\"insurancePrice\":{\"hyCostPrice\":0,\"hyPrice\":0.0,\"hySupportInsurance\":false,\"supportInsurance\":false,\"ywCostPrice\":0,\"ywPrice\":0.0,\"ywSupportInsurance\":false,\"zhCostPrice\":0,\"zhPrice\":0.0,\"zhSupportInsurance\":false},\"routeDetail\":{\"combineRebate\":0.0,\"fromSalePrice\":{\"adultTax\":304.00,\"rate\":1.0,\"adultPrice\":327.00,\"adultTaxType\":0,\"currency\":\"CNY\"},\"orderKey\":\"15304818672697529\",\"frBasicPriceMarkup\":{\"G7\":2,\"G8\":2,\"G0\":5,\"G1\":2,\"G2\":5,\"G3\":0,\"G4\":0,\"G5\":2,\"G6\":2},\"ticketSupplierCode\":\"HK1\",\"fromRoute\":{\"miniRule\":{\"adultMinirule\":{\"changeable\":false,\"refundable\":false},\"childMinirule\":{\"changeable\":false,\"refundable\":false}},\"data\":\"cZzAh9C1iJe2zIdlePnUmAlQKvjS5ETLLkAZg0CJsNmLk6cahIo7fDo4WZdMW6e2hgSD_Ks0JoRB10rxG_wotXBuSdGDJI6KorTLyNQysGE_kuAnfjAKwT3OyNvTvUxodK3bn01-k6Vlig6hbZeLJG0jZGBV4YKk3Ve__d1cooKJ6AfCa2acr27Gqnhb3ewoF-wktROWfElXjFxAfMSBjXwztR9fp-ubE-51luflbwH3nO47ykdgZC8SqcVmR4-jqLoaqCSaR6NsO7he0SSKIKzPHe6lp7DHZ0ueMUbUqNdvxHh41VqBU639VkuVmF0I1_STLQBj2Vu080Vk1IiXQ47pMGvkZXYR7TccGQL6Tdf9r5dZUkzULpkjXYqZpUWyGMApH-PbpWxvjq_OYOO5hIL0aCF7Vmaaq0faYpzRilk2cEtm2FjKdnuUNi9PXcNkcCDydtnulou67SNbA0iyASbLwYxjXMKvCY25hXe3XpVIF8pTYa_ImtmWsjGdXIWaNH9HuoMZ9VpiUs0PuKV52chkAY-9QVCNS3Z7vuLYoNPx6OiTmKwBetw2Ye1dlenlFgu_U2VmepKUw60cNr4jv8ZUQsIH01SfoEHVbqqeXU0Rqh4NAPIdbipi5F0m7I_MM5KWR3ztHXCyK6ZX9j40ViKEIsHOK6XHiJycJITb6SnYU7Iy7ajW37YbXFDWDCBxCVXaGbot1nsa5_TqKVyRrESlgXtnBiBB8Qv-77yTjDXaEb8izYaHqfWHzjrAh6xxiXeFTBL8_VWnMjg4Kx6lAgigAAnMSgln4iUeMQp7TblH-gb_jgjfXmE1NDiISE9xxNFrVchyNIm2H1BOhs8xbgzKsylGcaawcWWP3whBh7xeBF-KCscbAqhG8zials_tGK9mM1BBMly3Ml9Rqyyls7r0CgGLAaAOsOFp-btLZEHypZIZ5-W06jkfxT3OCLiVWpSaBjXFtnAEtBphBHf8t022v5WiQMUSOuW3EcnsByxiPCQdvkplcRuWyJGEdJeGq4WFIGWqlPeHDxjBI-7JZauWks3nnH1CfcJ5C71EuOxbkbl1ws6_jng4VeJCbMAd\",\"platingCarrier\":\"UO\",\"price\":{\"adultTax\":304,\"adultPrice\":327,\"adultTaxType\":0,\"currency\":\"CNY\"},\"supplierChannel\":\"OTA\",\"jcbPrice\":false,\"fromSegments\":[{\"departureTerminal\":\"\",\"arrivingTerminal\":\"1\",\"depTime\":\"202306301105\",\"cabinGrade\":\"Y\",\"airRailFlage\":false,\"childWeight\":\"0KG\",\"sumFlightTime\":\"2h45m\",\"cabin\":\"W\",\"depAirport\":\"BKK\",\"flightNumber\":\"UO705\",\"fromCityTimeZone\":\"GMT+0700\",\"carrier\":\"UO\",\"adultWeight\":\"0KG\",\"fareBasis\":\"WF1O\",\"codeShare\":false,\"aircraftCode\":\"321\",\"arrAirport\":\"HKG\",\"toCityTimeZone\":\"GMT+0800\",\"availSeatNum\":\"8\",\"stops\":0,\"flightTime\":\"2h45m\",\"arrTime\":\"202306301450\"}]},\"codeShareRebate\":0.0,\"bookingIncentive\":{\"G7\":0.0,\"G8\":0.0,\"G0\":0.0,\"G1\":0.0,\"G2\":0.0,\"G3\":0.0,\"G4\":0.0,\"G5\":0.0,\"G6\":0.0},\"rebateType\":\"GRADE\",\"ruleInfoDesc\":{\"baggageDesc\":\"无法获取行李额,如需购买请咨询客服人员。\",\"otherRefundDesc\":\"\",\"refundDesc\":\"不可退票、退税\",\"errorUpDesc\":\"不可废票\",\"changeDesc\":\"按航司改期规则执行\",\"ticketDesc\":\"境外系统出票\",\"endorseDesc\":\"不可签转\",\"otherDesc\":\"暂无\"},\"supplierMarkup\":0.0,\"baggageInfoList\":[{\"journey\":\"BKK-HKG\",\"orderId\":0,\"childBaggage\":\"0KG\",\"id\":0,\"adultBaggage\":\"0KG\"}],\"policyId\":\"11336271034671104\",\"policyRebate\":0.0,\"ruleInfo\":{\"frRuleInfo\":{\"baggageFee\":0,\"endorseFee\":50,\"refundFee\":50,\"errorUpFee\":0,\"currency\":\"CNY\",\"spoiledFee\":20,\"changeFee\":50},\"purchaserRuleInfo\":{\"baggageFee\":0.0,\"endorseFee\":50.0,\"refundFee\":50.0,\"purchaserCode\":\"XCJ\",\"errorUpFee\":0.0,\"currency\":\"CNY\",\"spoiledFee\":20.0,\"changeFee\":50.0}},\"frDetailPriceMarkup\":\"\",\"uniteFlightRebate\":0.0},\"carrierInfoMap\":{\"UO\":{\"carrierNameCn\":\"香港快运航空\",\"carrierNameEn\":\"Hong Kong Express Airways\",\"carrierTwoCode\":\"UO\"}},\"useBaggageList\":true,\"isAddSalePriceCid\":false,\"supportID\":false,\"isFareRulesShow\":false,\"cid\":\"XCJ\",\"status\":0}"));
}
public static String filter(String data){
if (StringUtils.isEmpty(data)){
return new String();
}
Map dataMap = (Map) JSONObject.parse(data);
String cid = (String) dataMap.get("cid");
if (ReadPropertiesUtils.containContent(properties,displaySupplierCodeList, cid)){
filterList.removeAll(ignoreFilterList);
}
return JSON.toJSONString(doFilter(dataMap,filterList));
}
private static Map doFilter(Map dataMap,List<String> filterList){
try {
Iterator var = dataMap.entrySet().iterator();
while (var.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) var.next();
String k = entry.getKey();
Object v = entry.getValue();
if (StringUtils.equals(k,"supplierChannel")){
SupplierChannelEnum channelEnum = SupplierChannelEnum.getByDesc((String) v);
dataMap.put(k,channelEnum == null ? v : channelEnum.getCode());
}
if (StringUtils.equals(k,"gdsType") || StringUtils.equals(k,"gds")){
try {
GdsType gdsType = GdsType.valueOf((String) v);
if (gdsType !=null){
dataMap.put(k,gdsType.getCode());
}
}catch (Exception e){
GdsType gds = GdsType.getByDesc((String) v);
if (gds !=null){
dataMap.put(k,gds.getCode());
}
}
}
if (filterList.contains(k)){
var.remove();
}else if (v instanceof Map){
doFilter((Map) v,filterList);
}else if (v instanceof List){
for (Object o : (List) v) {
if (o instanceof Map){
doFilter((Map)o,filterList);
}
}
}
}
}catch (Exception e){
log.error("vo data filter error:",e);
}
return dataMap;
}
public static String filter(String data,String filterStr,String ignoreFilterStr){
if (StringUtils.isEmpty(data)){
return new String();
}
if (StringUtils.isEmpty(filterStr)){
return data;
}
Map dataMap = (Map) JSONObject.parse(data);
List<String> filterList = Arrays.asList(filterStr.split(","));
if (StringUtils.isNotEmpty(ignoreFilterStr)){
List<String> ignoreFilterList = Arrays.asList(ignoreFilterStr.split(","));
String cid = (String) dataMap.get("cid");
if (ReadPropertiesUtils.containContent(properties,displaySupplierCodeList, cid)){
filterList.removeAll(ignoreFilterList);
}
}
return JSON.toJSONString(doFilter(dataMap,filterList));
}
}
将自定义注解作用在方法上进行返回数据过滤
问题点:作用在controller的方法不生效
解决办法:spring-mvc.xml中增加
<aop:aspectj-autoproxy proxy-target-class="true"/>