com.xiongyingqi.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.xiongyingqi.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java

Source

/*
 * Copyright 2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xiongyingqi.spring.mvc.method.annotation;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.xiongyingqi.spring.mvc.bind.annotation.RequestJsonParam;
import com.xiongyingqi.spring.mvc.util.MapWapper;
import com.xiongyingqi.util.EntityHelper;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver;

import javax.servlet.ServletException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * ??json
 * 
 * @author Zhang Kaitao
 * @since 3.1
 */
public class RequestJsonParamMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver
        implements WebArgumentResolver {
    private String encoding = "UTF-8";

    private ObjectMapper mapper = new ObjectMapper();

    public RequestJsonParamMethodArgumentResolver() {
        super(null);
    }

    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.hasParameterAnnotation(RequestJsonParam.class)) {
            return true;
        }
        return false;
    }

    @Override
    protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
        RequestJsonParam annotation = parameter.getParameterAnnotation(RequestJsonParam.class);
        return new RequestJsonParamNamedValueInfo(annotation);
    }

    @Override
    protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest)
            throws Exception {
        String[] paramValues = webRequest.getParameterValues(name);

        Class<?> paramType = parameter.getParameterType();
        if (paramValues == null) {
            return null;
        }

        // ?
        //      for (int i = 0; i < paramValues.length; i++) {
        //         String paramValue = paramValues[i];
        //         String encodedParamValue = new String(paramValue.getBytes("ISO-8859-1"), encoding);
        //         paramValues[i] = encodedParamValue;
        //      }
        try {
            if (paramValues.length == 1) {
                String text = paramValues[0];
                Type type = parameter.getGenericParameterType();

                if (MapWapper.class.isAssignableFrom(paramType)) {
                    MapWapper<?, ?> jsonMap = (MapWapper<?, ?>) paramType.newInstance();

                    MapType mapType = (MapType) getJavaType(HashMap.class);

                    if (type instanceof ParameterizedType) {
                        mapType = (MapType) mapType
                                .narrowKey((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]);
                        mapType = (MapType) mapType.narrowContentsBy(
                                (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1]);
                    }
                    jsonMap.setInnerMap(mapper.<Map>readValue(text, mapType));
                    return jsonMap;
                }

                JavaType javaType = getJavaType(paramType);

                if (Collection.class.isAssignableFrom(paramType)) {
                    javaType = javaType
                            .narrowContentsBy((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]);
                }
                return mapper.readValue(paramValues[0], javaType);
            }

        } catch (Exception e) {
            throw new JsonMappingException("Could not read request json parameter", e);
        }

        throw new UnsupportedOperationException("too many request json parameter '" + name
                + "' for method parameter type [" + paramType + "], only support one json parameter");
    }

    protected JavaType getJavaType(Class<?> clazz) {
        return TypeFactory.defaultInstance().constructRawMapType((Class<? extends Map>) clazz);
    }

    @Override
    protected void handleMissingValue(String paramName, MethodParameter parameter) throws ServletException {
        String paramType = parameter.getParameterType().getName();
        throw new ServletRequestBindingException(
                "Missing request json parameter '" + paramName + "' for method parameter type [" + paramType + "]");
    }

    private class RequestJsonParamNamedValueInfo extends NamedValueInfo {

        private RequestJsonParamNamedValueInfo() {
            super("", false, null);
        }

        private RequestJsonParamNamedValueInfo(RequestJsonParam annotation) {
            super(annotation.value(), annotation.required(), null);
        }
    }

    /**
     * spring 3.1?
     */
    public Object resolveArgument(MethodParameter parameter, NativeWebRequest request) throws Exception {
        String[] values = request.getParameterValues(parameter.getParameterName());
        for (String paramValue : values) {
            EntityHelper.print(paramValue);
        }
        if (!supportsParameter(parameter)) {
            return WebArgumentResolver.UNRESOLVED;
        }
        return resolveArgument(parameter, null, request, null);
    }

    /**
     * String
     * 
     * @return the encoding
     */
    public String getEncoding() {
        return encoding;
    }

    /**
     * String
     * 
     * @param encoding
     *            the encoding to set
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }
}