zoukankan      html  css  js  c++  java
  • 参数注解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");
        }
    
    }
  • 相关阅读:
    [Matlab] subplot 紧凑,减少空白
    论文
    统计学习理论
    位操作
    Pattern Recognition and Machine Learning
    HDU6862 Hexagon (2020HDU 多校 D8 H)
    VS2008 : error PRJ0002 : 错误的结果 1 (从“d:/Program Files/Microsoft Visual Studio 9.0/VC/bin/cl.exe”返回)
    static简单实例说明其用法(转)
    ArcGIS Engine开发系列:将地图导出为图片的两种方法
    按类别列出的编译器选项
  • 原文地址:https://www.cnblogs.com/koushr/p/11908010.html
Copyright © 2011-2022 走看看