Java Code Examples for org.springframework.web.bind.annotation.RequestParam#name()

The following examples show how to use org.springframework.web.bind.annotation.RequestParam#name() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestParamAnnotationProcessor.java    From servicecomb-toolkit with Apache License 2.0 6 votes vote down vote up
@Override
public void process(RequestParam requestParam, ParameterContext parameterContext) {

  parameterContext.setIn(InType.QUERY);
  String name = requestParam.value();
  if (StringUtils.isEmpty(name)) {
    name = requestParam.name();
  }

  parameterContext.setName(name);
  parameterContext.setRequired(requestParam.required());
  if (!ObjectUtils.isEmpty(requestParam.defaultValue()) && !ValueConstants.DEFAULT_NONE
      .equals(requestParam.defaultValue())) {
    parameterContext.setDefaultValue(requestParam.defaultValue());
    parameterContext.setRequired(false);
  }
}
 
Example 2
Source File: RequestParamMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void contributeMethodArgument(MethodParameter parameter, Object value,
		UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {

	Class<?> paramType = parameter.getParameterType();
	if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType ||
			"javax.servlet.http.Part".equals(paramType.getName())) {
		return;
	}

	RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
	String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name());

	if (value == null) {
		builder.queryParam(name);
	}
	else if (value instanceof Collection) {
		for (Object element : (Collection<?>) value) {
			element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
			builder.queryParam(name, element);
		}
	}
	else {
		builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
	}
}
 
Example 3
Source File: RequestParamMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void contributeMethodArgument(MethodParameter parameter, @Nullable Object value,
		UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {

	Class<?> paramType = parameter.getNestedParameterType();
	if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || Part.class == paramType) {
		return;
	}

	RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
	String name = (requestParam != null && StringUtils.hasLength(requestParam.name()) ?
			requestParam.name() : parameter.getParameterName());
	Assert.state(name != null, "Unresolvable parameter name");

	parameter = parameter.nestedIfOptional();
	if (value instanceof Optional) {
		value = ((Optional<?>) value).orElse(null);
	}

	if (value == null) {
		if (requestParam != null &&
				(!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE))) {
			return;
		}
		builder.queryParam(name);
	}
	else if (value instanceof Collection) {
		for (Object element : (Collection<?>) value) {
			element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
			builder.queryParam(name, element);
		}
	}
	else {
		builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
	}
}
 
Example 4
Source File: RequestParamMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void contributeMethodArgument(MethodParameter parameter, @Nullable Object value,
		UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {

	Class<?> paramType = parameter.getNestedParameterType();
	if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || Part.class == paramType) {
		return;
	}

	RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
	String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ?
			parameter.getParameterName() : requestParam.name());
	Assert.state(name != null, "Unresolvable parameter name");

	if (value == null) {
		if (requestParam != null &&
				(!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE))) {
			return;
		}
		builder.queryParam(name);
	}
	else if (value instanceof Collection) {
		for (Object element : (Collection<?>) value) {
			element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
			builder.queryParam(name, element);
		}
	}
	else {
		builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
	}
}
 
Example 5
Source File: RequestParamAnnotationProcessor.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public String getParameterName(RequestParam annotation) {
  String value = annotation.value();
  if (value.isEmpty()) {
    value = annotation.name();
  }
  return value;
}
 
Example 6
Source File: RequestParamMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void contributeMethodArgument(MethodParameter parameter, Object value,
		UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) {

	Class<?> paramType = parameter.getNestedParameterType();
	if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType ||
			"javax.servlet.http.Part".equals(paramType.getName())) {
		return;
	}

	RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
	String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ?
			parameter.getParameterName() : requestParam.name());

	if (value == null) {
		if (requestParam != null) {
			if (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) {
				return;
			}
		}
		builder.queryParam(name);
	}
	else if (value instanceof Collection) {
		for (Object element : (Collection<?>) value) {
			element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
			builder.queryParam(name, element);
		}
	}
	else {
		builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value));
	}
}
 
Example 7
Source File: ApiClientMethod.java    From onetwo with Apache License 2.0 4 votes vote down vote up
protected void handleArg(MultiValueMap<String, Object> values, ApiClientMethodParameter mp, final Object pvalue, boolean parameterNameAsPrefix){
		if(pvalue instanceof ApiArgumentTransformer){
			Object val = ((ApiArgumentTransformer)pvalue).asApiValue();
			values.add(mp.getParameterName(), val);
			return ;
		}
		
		String prefix = "";
		Object paramValue = pvalue;
		//下列情况,强制使用名称作为前缀
		if(mp.hasParameterAnnotation(RequestParam.class)){
			RequestParam params = mp.getParameterAnnotation(RequestParam.class);
			if(pvalue==null && params.required() && (paramValue=params.defaultValue())==ValueConstants.DEFAULT_NONE){
				throw new BaseException("parameter["+params.name()+"] must be required : " + mp.getParameterName());
			}
			parameterNameAsPrefix = true;
		}else if(isUriVariables(mp) || mp.hasParameterAnnotation(FieldName.class)){
			parameterNameAsPrefix = true;
		}else if(beanToMapConvertor.isMappableValue(pvalue)){//可直接映射为值的参数
			parameterNameAsPrefix = true;
		}
		
		if(parameterNameAsPrefix){
			prefix = mp.getParameterName();
		}
		beanToMapConvertor.flatObject(prefix, paramValue, (k, v, ctx)->{
			values.add(k, v);
		});
		
		/*if(falatable){
//			beanToMapConvertor.flatObject(mp.getParameterName(), paramValue, (k, v, ctx)->{
			beanToMapConvertor.flatObject(mp.getParameterName(), paramValue, (k, v, ctx)->{
				if(v instanceof Enum){
					Enum<?> e = (Enum<?>)v;
					if(e instanceof ValueEnum){
						v = ((ValueEnum<?>)e).getValue();
					}else{//默认使用name
						v = e.name();
					}
				}else if(v instanceof Resource){
					//ignore,忽略,不转为string
				}else{
					v = v.toString();
				}
				if(ctx!=null){
//					System.out.println("ctx.getName():"+ctx.getName());
					values.add(ctx.getName(), v);
				}else{
					values.add(k, v);
				}
//				values.add(k, v);
			});
		}else{
			values.add(mp.getParameterName(), pvalue);
		}*/
	}
 
Example 8
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod,
		WebDataBinder binder, NativeWebRequest webRequest) throws Exception {

	Class<?>[] initBinderParams = initBinderMethod.getParameterTypes();
	Object[] initBinderArgs = new Object[initBinderParams.length];

	for (int i = 0; i < initBinderArgs.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i);
		methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
		GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
		String paramName = null;
		boolean paramRequired = false;
		String paramDefaultValue = null;
		String pathVarName = null;
		Annotation[] paramAnns = methodParam.getParameterAnnotations();

		for (Annotation paramAnn : paramAnns) {
			if (RequestParam.class.isInstance(paramAnn)) {
				RequestParam requestParam = (RequestParam) paramAnn;
				paramName = requestParam.name();
				paramRequired = requestParam.required();
				paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
				break;
			}
			else if (ModelAttribute.class.isInstance(paramAnn)) {
				throw new IllegalStateException(
						"@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod);
			}
			else if (PathVariable.class.isInstance(paramAnn)) {
				PathVariable pathVar = (PathVariable) paramAnn;
				pathVarName = pathVar.value();
			}
		}

		if (paramName == null && pathVarName == null) {
			Object argValue = resolveCommonArgument(methodParam, webRequest);
			if (argValue != WebArgumentResolver.UNRESOLVED) {
				initBinderArgs[i] = argValue;
			}
			else {
				Class<?> paramType = initBinderParams[i];
				if (paramType.isInstance(binder)) {
					initBinderArgs[i] = binder;
				}
				else if (BeanUtils.isSimpleProperty(paramType)) {
					paramName = "";
				}
				else {
					throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
							"] for @InitBinder method: " + initBinderMethod);
				}
			}
		}

		if (paramName != null) {
			initBinderArgs[i] =
					resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null);
		}
		else if (pathVarName != null) {
			initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null);
		}
	}

	return initBinderArgs;
}
 
Example 9
Source File: RequestParamMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 10
Source File: WxArgumentResolver.java    From FastBootWeixin with Apache License 2.0 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
    super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 11
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod,
		WebDataBinder binder, NativeWebRequest webRequest) throws Exception {

	Class<?>[] initBinderParams = initBinderMethod.getParameterTypes();
	Object[] initBinderArgs = new Object[initBinderParams.length];

	for (int i = 0; i < initBinderArgs.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i);
		methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
		GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
		String paramName = null;
		boolean paramRequired = false;
		String paramDefaultValue = null;
		String pathVarName = null;
		Annotation[] paramAnns = methodParam.getParameterAnnotations();

		for (Annotation paramAnn : paramAnns) {
			if (RequestParam.class.isInstance(paramAnn)) {
				RequestParam requestParam = (RequestParam) paramAnn;
				paramName = requestParam.name();
				paramRequired = requestParam.required();
				paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
				break;
			}
			else if (ModelAttribute.class.isInstance(paramAnn)) {
				throw new IllegalStateException(
						"@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod);
			}
			else if (PathVariable.class.isInstance(paramAnn)) {
				PathVariable pathVar = (PathVariable) paramAnn;
				pathVarName = pathVar.value();
			}
		}

		if (paramName == null && pathVarName == null) {
			Object argValue = resolveCommonArgument(methodParam, webRequest);
			if (argValue != WebArgumentResolver.UNRESOLVED) {
				initBinderArgs[i] = argValue;
			}
			else {
				Class<?> paramType = initBinderParams[i];
				if (paramType.isInstance(binder)) {
					initBinderArgs[i] = binder;
				}
				else if (BeanUtils.isSimpleProperty(paramType)) {
					paramName = "";
				}
				else {
					throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
							"] for @InitBinder method: " + initBinderMethod);
				}
			}
		}

		if (paramName != null) {
			initBinderArgs[i] =
					resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null);
		}
		else if (pathVarName != null) {
			initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null);
		}
	}

	return initBinderArgs;
}
 
Example 12
Source File: RequestParamMethodArgumentResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 13
Source File: OpenFeignApplictionInitalizer.java    From summerframework with Apache License 2.0 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
    super((annotation.name() != null && annotation.name() != "") ? annotation.name() : annotation.value(),
        annotation.required(), annotation.defaultValue());

}
 
Example 14
Source File: RequestParamMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 15
Source File: RequestParamMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 16
Source File: RequestParamMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}
 
Example 17
Source File: RequestParamMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
RequestParamNamedValueInfo(RequestParam annotation) {
	super(annotation.name(), annotation.required(), annotation.defaultValue());
}