• 参数注解2


    定义注解类:

    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface JsonParam {
        
        String value() default "";
    
        boolean required() default true;
    
        String defaultValue() default ValueConstants.DEFAULT_NONE;
    }

    定义解析器:

    public class JsonParamAnnotationResolver implements HandlerMethodArgumentResolver {
    
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.getParameterAnnotation(JsonParam.class) != null;
        }
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mvContainer,
                NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
            JsonParam jsonParam = parameter.getParameterAnnotation(JsonParam.class);
            if (jsonParam == null) {
                throw new Exception("参数无效");
            }
            Object result;
            result = resolveArgument0(parameter, jsonParam);
            if (result == null && jsonParam.required()) {
                throw new Exception("参数无效");
            }
            return result;
        }
    
        private Object resolveArgument0(MethodParameter parameter, JsonParam jsonParam) {
            String paramName = jsonParam.value();
            if (parameter.getParameterType() == List.class) {
                return getJsonList(paramName);
            } else if (parameter.getParameterType() == Map.class) {
                return getJsonMap(paramName);
            } else {
                Class<?> type = parameter.getParameterType();
                Object value = getJson(paramName, type);
                if (value != null) {
                    return value;
                }
                if (!StringUtils.equals(jsonParam.defaultValue(), DEFAULT_HOLDER)) {
                    return ObjectMapperUtils.value(jsonParam.defaultValue(), type);
                }
                return null;
            }
        }
    
        private static <T> T getJson(String key, Class<T> type) {
            Object value = WebScope.getJsonParam(key);
            return ObjectMapperUtils.value(value, type);
        }
    
        private static <T> List<T> getJsonList(String key) {
            return WebScope.getJsonParam(key);
        }
    
        private static <K, V> Map<K, V> getJsonMap(String key) {
            return WebScope.getJsonParam(key);
        }
    }

    工具类:

    public class WebScopeUtils {
    
        private static final Map<String, Object> JSON_PARAMS = parseRequestJson();
    
        public static <T> T getJsonParam(String name) {
            return (T) JSON_PARAMS.get(name);
        }
    
        private static Map<String, Object> parseRequestJson() {
            HttpServletRequest request =
                    ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
            if (!isJsonRequest(request)) {
                return Collections.emptyMap();
            }
            try (InputStream input = request.getInputStream()) {
                byte[] bytes = IOUtils.toByteArray(input);
                String encoding = StringUtils.defaultIfBlank(request.getCharacterEncoding(), "UTF-8");
                String content = new String(bytes, encoding);
                return ObjectMapperUtils.fromJson(content);
            } catch (Exception e) {
                return Collections.emptyMap();
            }
        }
    
        private static boolean isJsonRequest(HttpServletRequest request) {
            String contentType = request.getHeader("Content-Type");
            return contentType != null && contentType.toLowerCase().contains("application/json");
        }
    
    }
  • 相关阅读:
    [设计模式]门面模式
    [设计模式]装饰者模式
    IE8半透明不显示出文字
    解决IE6,IE7不能隐藏绝对定位溢出的内容
    [设计模式]适配器模式
    linux学习网站
    PPP协议解析
    c语言面试题__指针篇
    单链表反转
    16道C语言面试题
  • 原文地址:https://www.cnblogs.com/koushr/p/11908010.html
Copyright © 2020-2023  润新知