Search in sources :

Example 1 with SingleRequestBody

use of com.jeeagile.frame.support.resolver.annotation.SingleRequestBody in project jeeagile by jeeagile.

the class SingleRequestBodyResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {
    SingleRequestBody singleRequestBody = methodParameter.getParameterAnnotation(SingleRequestBody.class);
    Assert.state(singleRequestBody != null, "Unresolvable singleRequestBody");
    final String httpRequestBody = getHttpRequestBody(nativeWebRequest);
    Assert.state(httpRequestBody != null, "Unresolvable httpRequestBody");
    JSONObject jsonObject = getJsonObjectByHttpRequestBody(httpRequestBody);
    String parameterName = AgileStringUtil.isEmpty(singleRequestBody.name()) ? methodParameter.getParameterName() : singleRequestBody.name();
    Assert.state(parameterName != null, "Unresolvable parameter name");
    Class<?> parameterType = methodParameter.getParameterType();
    Object object = null;
    if (jsonObject == null) {
        object = getValueObjectByHttpRequestBody(parameterType, httpRequestBody);
    } else {
        object = getValueObjectByJsonObject(methodParameter, parameterName, jsonObject);
    }
    if (singleRequestBody.required()) {
        if (object == null) {
            throw new AgileFrameException(String.format("required param %s is not present", parameterName));
        }
    } else {
        if (object == null && !singleRequestBody.defaultValue().equals(ValueConstants.DEFAULT_NONE)) {
            if (isBasicDataTypes(parameterType)) {
                // 基本类型包装类
                object = parseBasicTypeWrapper(parameterType, singleRequestBody.defaultValue());
            } else if (parameterType == String.class) {
                // 字符串类型
                object = singleRequestBody.defaultValue();
            }
        }
    }
    return object;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) SingleRequestBody(com.jeeagile.frame.support.resolver.annotation.SingleRequestBody)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 AgileFrameException (com.jeeagile.core.exception.AgileFrameException)1 SingleRequestBody (com.jeeagile.frame.support.resolver.annotation.SingleRequestBody)1