Example usage for org.springframework.web.context.request NativeWebRequest getParameterValues

List of usage examples for org.springframework.web.context.request NativeWebRequest getParameterValues

Introduction

In this page you can find the example usage for org.springframework.web.context.request NativeWebRequest getParameterValues.

Prototype

@Nullable
String[] getParameterValues(String paramName);

Source Link

Document

Return the request parameter values for the given parameter name, or null if none.

Usage

From source file:net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolverTest.java

@Test
public void resolvesJoinFetchForSimpleSpec() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isInstanceOf(Conjunction.class);

    Collection<Specification<?>> innerSpecs = ReflectionUtils.get(resolved, "innerSpecs");

    assertThat(innerSpecs).hasSize(2).contains(new Like<Object>("path1", new String[] { "value1" })).contains(
            new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1", "fetch2" },
                    JoinType.LEFT));//from ww  w.  j av  a 2 s . c om
}

From source file:com.sws.platform.mobile.common.json.RequestJsonParamMethodArgumentResolver.java

@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;
    }/*  w w  w  .j a v a 2 s . c om*/
    //?json
    String data = webRequest.getParameter(name);
    if (StringUtil.isNullOrEmptyOrWhiteSpace(data)) {
        return null;
    }

    //        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 JSONArray.parseArray(paramValues[0],
                (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]);
    }

    return JSON.parseObject(paramValues[0], type, this.fastJsonConfig.getFeatures());
    //        }

    //        } catch (Exception e) {
    //            throw new JSONException("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");
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SpecificationArgumentResolverTest.java

@Test
public void resolvesJoinContainer() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod_joinContainer"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });

    Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isInstanceOf(Conjunction.class);

    Collection<Specification<?>> innerSpecs = ReflectionUtils.get(resolved, "innerSpecs");

    assertThat(innerSpecs).hasSize(2).contains(new Like<Object>("path1", new String[] { "value1" }))
            .contains(new Conjunction<Object>(
                    new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1" },
                            JoinType.LEFT),
                    new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch2" },
                            JoinType.INNER)));
}

From source file:base.resolver.JsonModelResolver.java

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;

    }//from  w  ww .j a  v  a2  s .  com

    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");
}

From source file:cn.javass.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java

@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;

    }/*from w  w  w  .j  a  va  2 s  .  com*/

    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");
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_defaultParameterName() {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod3"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });

    assertThat(resolver.buildSpecification(req, param.getParameterAnnotation(Spec.class))).isNull();
    ;//from   ww  w  . j  a  v  a  2  s.  com
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_customParameterName() {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });

    assertThat(resolver.buildSpecification(req, param.getParameterAnnotation(Spec.class))).isNull();
    ;/*from   w w  w. j a v  a 2 s.co  m*/
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfTheWebParameterIsEmpty_defaultParameterName() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod1"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "" });

    Specification<?> resolved = resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isNull();/*from w  ww .ja v  a 2  s . c  om*/
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void returnsNullIfTheWebParameterIsEmpty_customParameterName() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod2"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("theParameter")).thenReturn(new String[] { "" });

    Specification<?> resolved = resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isNull();/*from ww  w .  j  a va2s. co  m*/
}

From source file:net.kaczmarzyk.spring.data.jpa.web.SimpleSpecificationResolverTest.java

@Test
public void buildsTheSpecUsingWebParameterTheSameAsPath() throws Exception {
    MethodParameter param = MethodParameter.forMethodOrConstructor(testMethod("testMethod1"), 0);
    NativeWebRequest req = mock(NativeWebRequest.class);
    when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue" });

    Specification<?> resolved = resolver.resolveArgument(param, null, req, null);

    assertThat(resolved).isEqualTo(new Like<>("thePath", new String[] { "theValue" }));
}