package mingzuozhibi.support;

import com.jayway.jsonpath.JsonPath;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class JsonArgumentResolver implements HandlerMethodArgumentResolver {

    private static final String JSON_BODY_ATTRIBUTE = "JSON_REQUEST_BODY";

    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(JsonArg.class);
    }

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String body = getRequestBody(webRequest);
        String arg = parameter.getParameterAnnotation(JsonArg.class).value();
        if (StringUtils.isEmpty(arg)) {
            arg = parameter.getParameterName();
        }
        return JsonPath.parse(body).read(arg, parameter.getParameterType());
    }

    private String getRequestBody(NativeWebRequest webRequest) {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);

        String jsonBody = (String) webRequest.getAttribute(JSON_BODY_ATTRIBUTE, NativeWebRequest.SCOPE_REQUEST);
        if (jsonBody == null) {
            try {
                jsonBody = IOUtils.toString(servletRequest.getInputStream(), "UTF-8");
                webRequest.setAttribute(JSON_BODY_ATTRIBUTE, jsonBody, NativeWebRequest.SCOPE_REQUEST);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return jsonBody;
    }

}

@@ -1290,76 +1290,353 @@
     
-return JsonPath.parse(body).read(arg, parameter.getParameterType());
+Class%3C?%3E parameterType = parameter.getParameterType();%0A        if (parameterType.isEnum()) %7B%0A            String read = JsonPath.parse(body).read(arg, String.class);%0A            return parameterType.getMethod(%22valueOf%22, String.class).invoke(null, read);%0A        %7D else %7B%0A            return JsonPath.parse(body).read(arg, parameterType);%0A        %7D
 %0A   

Support for Enum