摘要:前言和切面一樣,在項(xiàng)目中同樣使用了自定義注解,目前項(xiàng)目中使用的自定義注解主要分為以下一些方面參數(shù)解析,緩存方法聲明,導(dǎo)入功能中的聲明。
前言
和切面一樣,在項(xiàng)目中同樣使用了自定義注解,目前項(xiàng)目中使用的自定義注解主要分為以下一些方面:controller參數(shù)解析,緩存方法聲明,導(dǎo)入功能中的POJO聲明。
@JsonObject用在controller的方法參數(shù),解析前臺(tái)提交的json參數(shù),組裝成java對(duì)象。用法如下:
@PostMapping("/bas/user/status/toggle") public PackVo toggleUserStatus(@JsonObject Long[] userIds , @JsonObject Long status ){ ... }
注解聲明
@Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface JsonObject { String value() default ""; boolean required() default true; }
注解處理,實(shí)現(xiàn)HandlerMethodArgumentResolver,
public class JsonArgumentResolver implements HandlerMethodArgumentResolver { public static Logger logger = LoggerFactory.getLogger(JsonArgumentResolver.class); @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.hasParameterAnnotation(JsonObject.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { try { JSONObject para = getRequestInfo(nativeWebRequest); Class> type = methodParameter.getParameterType(); String name = methodParameter.getParameterName(); //獲取參數(shù)注解JsonObject,如果為空,則使用參數(shù)名,不為空,則使用注解中的value Annotation[] annotations = methodParameter.getParameterAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof JsonObject){ String value = ((JsonObject) annotation).value(); if(StringUtils.isNotEmpty(value)){ name = value; } } } if (null != para && para.containsKey(name)) { if (String.class.equals(type)) { return para.getString(name); } if (List.class.equals(type)) { String typeName = methodParameter.getGenericParameterType().getTypeName(); typeName = typeName.substring(typeName.indexOf("<")+1); typeName =typeName.substring(0,typeName.length()-1); return JSON.parseArray(para.getString(name), Class.forName(typeName)); } return JSON.parseObject(para.getString(name), type); } } catch (Exception e) { e.printStackTrace(); logger.info("",e); } return null; } private JSONObject getRequestInfo(NativeWebRequest webRequest) throws IOException { JSONObject para = new JSONObject(); HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class); String method = httpServletRequest.getMethod(); if (!method.equals("GET") && !method.equals("DELETE")) { if (null != httpServletRequest.getAttribute("para")) { try { para = JSON.parseObject(httpServletRequest.getAttribute("para").toString()); } catch (Exception e) { e.printStackTrace(); logger.info("",e); } } else { StringBuilder buffer = new StringBuilder(); BufferedReader reader = httpServletRequest.getReader(); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } httpServletRequest.setAttribute("para", buffer.toString()); try { para = JSON.parseObject(buffer.toString()); } catch (Exception e) { e.printStackTrace(); logger.info("",e); } } } else { Map@RedisCacheparameterMap = webRequest.getParameterMap(); for (Map.Entry entry : parameterMap.entrySet()) { String key = entry.getKey(); String values = StringUtils.join(entry.getValue()); para.put(key, values); } } return para; } }
接口使用方式如下:
注解聲明,具體的注解處理在上一篇的DAO切面中有寫(xiě)到。
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RedisCache { String value() default ""; boolean required() default true; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/68466.html
摘要:前言目前系統(tǒng)剛啟動(dòng),骨架剛剛搭建完成,在項(xiàng)目中,使用了一些切面,做一些業(yè)務(wù)無(wú)關(guān)的處理。在現(xiàn)在的項(xiàng)目里面,分別在,調(diào)用,分頁(yè),處理,均使用到了切面。希望本文的閱讀者也不吝將項(xiàng)目中使用的切面分享出來(lái)。 前言 目前系統(tǒng)剛啟動(dòng),骨架剛剛搭建完成,在項(xiàng)目中,使用了一些切面,做一些業(yè)務(wù)無(wú)關(guān)的處理。在本文中,將各個(gè)切面例舉出來(lái),用以加深自己對(duì)切面的理解。記得在初學(xué)切面的時(shí)候,一般文章介紹切面的時(shí)候,...
摘要:示例代碼如下添加的設(shè)置默認(rèn)的配置對(duì)應(yīng)的是原來(lái)的如何使用注解從主庫(kù)到備庫(kù)的切換 摘要: 本篇文章的場(chǎng)景是做調(diào)度中心和監(jiān)控中心時(shí)的需求,后端使用TDDL實(shí)現(xiàn)分表分庫(kù),需求:實(shí)現(xiàn)關(guān)鍵業(yè)務(wù)的查詢(xún)監(jiān)控,當(dāng)用Mybatis查詢(xún)數(shù)據(jù)時(shí)需要從主庫(kù)切換到備庫(kù)或者直接連到備庫(kù)上查詢(xún),從而減小主庫(kù)的壓力,在本篇文章中主要記錄在Spring Boot中通過(guò)自定義注解結(jié)合AOP實(shí)現(xiàn)直接連接備庫(kù)查詢(xún)。 一.通過(guò)A...
摘要:生產(chǎn)環(huán)境服務(wù)器環(huán)境搭建安裝安裝在系統(tǒng)中通過(guò)以下命令輸入查看是否安裝正確,輸出如下創(chuàng)建發(fā)布目錄,并給出相應(yīng)的權(quán)限服務(wù)器和后臺(tái)文件夾上傳前端文件夾改變文件所有權(quán)文件的所有權(quán)變?yōu)榧尤雲(yún)?shù),文件夾以及文件夾里的所有 生產(chǎn)環(huán)境服務(wù)器環(huán)境搭建 安裝jdk 安裝 openjdk-8-jdk 在 ubuntu-16.04 系統(tǒng)中通過(guò)以下命令: 1.sudo add-apt-repository pp...
閱讀 2463·2021-10-13 09:40
閱讀 3347·2019-08-30 13:46
閱讀 1132·2019-08-29 14:05
閱讀 2966·2019-08-29 12:48
閱讀 3668·2019-08-26 13:28
閱讀 2158·2019-08-26 11:34
閱讀 2295·2019-08-23 18:11
閱讀 1172·2019-08-23 12:26