需求
地區資料往往是存在強上下級關系的一種資料結構,在電商系統中是比較常應用到的,比如北京的下級地區只有海淀區、通州區……,而不會是太原市,而且在開發人員傳遞地區值的時候往往要傳遞很多的值,比如省、市、區、鎮、省Id,市id、區id、鎮id,這樣影響了代碼的美觀性及校驗強上下級關系代碼的復雜性,基于這樣的問題要求我們必須要實作一種可以簡潔傳遞引數并且可以實作地區格式化的一種格式化器,這樣衍生出了我們基于Spring Formatter地區自動化格式的一種格式化器,以下是Javashop電商系統中具體的原始碼分享,
預期效果
常規做法的地區引數傳遞:
?province_id=1&county_id=2&city_id=3&town_id=4
在controller接收引數時:
@PostMapping
public Result someMethod(Integer provinceId,Integer countyId,Integer cityId,Integer townId){ //街道id不為空找到級聯的父 if(townId!=null){ //找到區 countyId=findCounty(townId) //找到市 cityId=findCity(countyId); //找到省 provinceId=findProvince(cityId); } //如果是只保存到區的需求,也要依次尋找級聯父 if(countyId!=null){ //找到市 cityId=findCity(countyId); //找到省 provinceId=findCity(cityId); } //如果是保存到市也是同樣,不在贅述 }
使用注解后效果:
引數傳遞:
xxx?region=3
在controller接收引數時
public result someMethod(@RegionFormat Region region) {
//這里的region物件中的省市區街道已經按相應的級別對應好,直接使用即可
if(region.getProvinceProId!=null){ // 保存省id}
if(region.getCityId!=null){ // 保存市id}
if(region.getCountyId=!=null{//保存區id}
if(region.getTwonId=!=null{//保存街道id}
}
根據上述需求,如下是基于javashop電商系統中,地區引數接收注解的架構和實作:
類圖

1、 RegionFormatter地區格式化器實作RegionFomat注解介面,實作地區轉換器方法pase方法,傳入地區id和地區語言,最后將轉換后的資料輸出,如下代碼
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface RegionFormat {
}
public class RegionFormatter implements Formatter<Region> {
private RegionsClient regionsClient;
public RegionFormatter(RegionsClient regionsClient) {
this.regionsClient = regionsClient;
}
public RegionFormatter() {
}
/**
* 地區轉換器
*
* @param regionId 地區id(只允許第三級或者第四級地區id)
* @param locale 國際化US
* @return 地區物件
* @throws ParseException
*/
@Override
public Region parse(String regionId, Locale locale) throws ParseException {
Regions regions = regionsClient.getModel(Integer.valueOf(regionId));
if (regions == null || regions.getRegionGrade() < 3) {
throw new IllegalArgumentException("地區不合法,請聯系管理員");
}
//根據底層地區id反推出上級id
String regionPath = regions.getRegionPath();
regionPath = regionPath.substring(1, regionPath.length());
String[] regionPathArray = regionPath.split(",");
//給地區賦值
List rList = new ArrayList();
for (String path : regionPathArray) {
Regions region = regionsClient.getModel(Integer.valueOf(path));
if (regions == null) {
throw new IllegalArgumentException("地區不合法,請聯系管理員");
}
rList.add(region);
}
return this.createRegion(rList);
}
/**
* 組織地區資料
*
* @param list 地區集合
* @return 地區
*/
private Region createRegion(List<Regions> list) {
//將地區資料組織好存入Region物件
Region region = new Region();
region.setProvinceId(list.get(0).getId());
region.setProvince(list.get(0).getLocalName());
region.setCityId(list.get(1).getId());
region.setCity(list.get(1).getLocalName());
region.setCountyId(list.get(2).getId());
region.setCounty(list.get(2).getLocalName());
//如果地區資料為四級,為第四級地區賦值
if (list.size() == 4) {
region.setTown(list.get(3).getLocalName());
region.setTownId(list.get(3).getId());
} else {
region.setTown("");
region.setTownId(0);
}
return region;
}
/**
* 將格式化的地區toString輸出
*
* @param object 地區物件
* @param locale 國際化US
* @return
*/
@Override
public String print(Region object, Locale locale) {
return object.toString();
}
}
2、 RegionFormatAnnotationFormatterFactory地區格式化工廠實作AnnotationFormatterFactory自定義格式工廠介面,將要實作的自動注解RegionFomat傳入,如下代碼:
public class RegionFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<RegionFormat> {
/**
* 獲取被注解物件的型別
*
* @return
*/
@Override
public Set<Class<?>> getFieldTypes() {
return new HashSet<Class<?>>(asList(Region.class));
}
/**
* 獲取輸出物件
*
* @param annotation 注解實體
* @param fieldType 被注解欄位的型別
* @return 地區格式化后輸出的物件
*/
@Override
public Printer<?> getPrinter(RegionFormat annotation, Class<?> fieldType) {
return new RegionFormatter();
}
/**
* 獲取決議器
*
* @param annotation 注解實體
* @param fieldType 被注解欄位的型別
* @return 地區格式化后物件
*/
@Override
public Parser<?> getParser(RegionFormat annotation, Class<?> fieldType) {
return new RegionFormatter();
}
}
3、 WebConfig實作WebMvcConfigurer,注冊自定義的地區格式化器,如下代碼:
public class RegionFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<RegionFormat> {
/**
* 獲取被注解物件的型別
*
* @return
*/
@Override
public Set<Class<?>> getFieldTypes() {
return new HashSet<Class<?>>(asList(Region.class));
}
/**
* 獲取輸出物件
*
* @param annotation 注解實體
* @param fieldType 被注解欄位的型別
* @return 地區格式化后輸出的物件
*/
@Override
public Printer<?> getPrinter(RegionFormat annotation, Class<?> fieldType) {
return new RegionFormatter();
}
/**
* 獲取決議器
*
* @param annotation 注解實體
* @param fieldType 被注解欄位的型別
* @return 地區格式化后物件
*/
@Override
public Parser<?> getParser(RegionFormat annotation, Class<?> fieldType) {
return new RegionFormatter();
}
}
使用
1、支持物件屬性注解
@RegionFormat
@ApiModelProperty(name = "region", value = "https://www.cnblogs.com/javashop-docs/p/地區")
private Region region;
2、值Controller入參注解
@ApiOperation(value = "https://www.cnblogs.com/javashop-docs/p/平臺添加店鋪", response = ShopVO.class)
@PostMapping()
public ShopVO save(@Valid ShopVO shop,
@RegionFormat @RequestParam("license_region") Region licenseRegion,
@RegionFormat @RequestParam("bank_region") Region bankRegion,
@RegionFormat @RequestParam("shop_region") Region shopRegion) {
}
效果
使用上述地區格式化器方法,假設如下請求
xxx?region=3
則會將id為1的地區及父的資訊讀取并且形成Region物件,
例如region=3的地區為長安街,父級為東城區->北京,形成的Region物件如下,
{
"cityId": 2,
"townId": 0,
"countyId": 3,
"provinceId": 1,
"province": "長安街",
"county": "東城區",
"city": "北京市",
"town": ""
}
如果傳遞的是townId,則會自動填充county、city及province,
如果傳遞的是countyId,則會自動填充city、province,此時townId為0,
同時支持物件屬性級別注解:
物件:
public Person{
//所屬區域
@RegionFormat
private Region region;
//geter and seter
}
controller:
@PostMapping()
public Result save(Person person) {
//這里的地區已經按上述規則賦值
person.getRegion()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/16288.html
標籤:架構設計
下一篇:分布式系統的CAP定理
