参数注解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");
    }

}
原文地址:https://www.cnblogs.com/koushr/p/11908010.html