Announcement Announcement Module
Collapse
No announcement yet.
Json in controller parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Json in controller parameters

    I want to have parameters in controllers declared as follows:
    ... method(@JsonIn Bean d, @JsonIn Bean d2, String s)
    or
    ... method(@JsonIn @RequestParam("d") Bean d, @JsonIn @RequestParam("d2") Bean d2, @RequestParam("s") String s)
    and want to call such method :
    ?d={id:"1", value:"v"}&d2={id:"1", value:"v"}&s="s"

    i tried to solve this by aspect

    Code:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface JsonIn {
    
    }
    
    @Component
    @Aspect
    public class RepairParametersAspect {
        private Logger logger = Logger.getLogger(this.getClass());
    
        /**
         * Метод преобразовывающий массивы нулевой длины в массивы с одним null.
         * 
         * @param pjp точка соприкосновения
         * @return аргумента метода
         * @throws Throwable 
         */
        @Around("execution(@org.springframework.web.bind.annotation.RequestMapping * *(..))")
        public Object callMethod(ProceedingJoinPoint pjp) throws Throwable{
            Object[] args = pjp.getArgs();
            MethodSignature s = (MethodSignature) pjp.getSignature();
            Annotation[][] pa = s.getMethod().getParameterAnnotations();
            Class[] parTypes = s.getParameterTypes();
            if(args!=null && args.length>0){
                for(int i=0;i<args.length;i++){
                     if(pa[i][0].annotationType().equals(JsonIn.class)){
                        logger.debug("args[i]="+args[i]);
                        logger.debug("parTypes[i]="+parTypes[i]);
                        args[i] = SiteUtil.gson.fromJson(""+args[i], parTypes[i]);
                    }
                }
                return pjp.proceed(args);
            } else {
                return pjp.proceed();
            }
        }
        
    }
    But logger.debug("args[i]="+args[i]); outputs
    args[i]=Bean{key=null, value=null}
    not String {id:"1", value:"v"}

    can it be done by aspects? or where to look?
Working...
X