@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)) { return; } WxApiParam wxApiParam = parameter.getParameterAnnotation(WxApiParam.class); String name = (wxApiParam == null || StringUtils.isEmpty(wxApiParam.name()) ? parameter.getParameterName() : wxApiParam.name()); WxAppAssert.notNull(name, "请添加编译器的-parameter或者为参数添加注解名称"); if (value == null) { if (wxApiParam != null) { if (!wxApiParam.required() || !wxApiParam.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)); } }
/** * Create a new NamedValueInfo based on the given NamedValueInfo with sanitized values. */ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { String name = info.name; if (info.name.length() == 0) { name = parameter.getParameterName(); Assert.notNull(name, "Name for argument type [" + parameter.getParameterType().getName() + "] not available, and parameter name information not found in class file either."); } String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue); return new NamedValueInfo(name, info.required, defaultValue); }
public RequestParamResolveBean(Class<?> clazz, String paraName, boolean required, String defaultValue, DataConverter dataConverter) { this.clazz = clazz; this.paraName = paraName; this.required = required; if (ValueConstants.DEFAULT_NONE.equals(defaultValue)) { this.defaultValue = dataConverter.defaultValue(); } else if (StringUtils.isNotEmpty(defaultValue)) { this.defaultValue = dataConverter.toObject(defaultValue); } else { this.defaultValue = dataConverter.defaultValue(); } this.dataConverter = dataConverter; this.isReference = dataConverter instanceof ReferenceDataConverter; }
private String getMethodWxApiRequestPath(Method method) { WxApiRequest wxApiRequest = AnnotatedElementUtils.findMergedAnnotation(method, WxApiRequest.class); if (wxApiRequest == null || StringUtils.isEmpty(wxApiRequest.path()) || ValueConstants.DEFAULT_NONE.equals(wxApiRequest.path())) { // 默认情况下取方法名为变量名,尝试从环境变量中获取信息 return WxContextUtils.resolveStringValue("${" + this.wxApiTypeInfo.getPropertyPrefix() + "." + method.getName() + "}"); } return wxApiRequest.path(); }
/** * 要发送文件,使用这种方式,请查看源码:FormHttpMessageConverter * * @param wxApiMethodInfo * @param args * @return dummy */ private Object getFormBody(WxApiMethodInfo wxApiMethodInfo, Object[] args) { MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); wxApiMethodInfo.getMethodParameters().stream() .filter(p -> !BeanUtils.isSimpleValueType(p.getParameterType()) || p.hasParameterAnnotation(WxApiForm.class) || p.hasParameterAnnotation(WxApiBody.class)) .forEach(p -> { // 为空则直接返回不加这个参数 if (args[p.getParameterIndex()] == null) { return; } WxApiForm wxApiForm = p.getParameterAnnotation(WxApiForm.class); String paramName; Object param; if (wxApiForm == null || ValueConstants.DEFAULT_NONE.equals(wxApiForm.value())) { paramName = p.getParameterName(); } else { paramName = wxApiForm.value(); } // 加入Assert WxAppAssert.notNull(paramName, "请添加编译器的-parameter或者为参数添加注解名称"); if (WxWebUtils.isMutlipart(p.getParameterType())) { param = getFormResource(args[p.getParameterIndex()]); } else { param = args[p.getParameterIndex()]; } params.add(paramName, param); }); return params; }
/** * Create a new NamedValueInfo based on the given NamedValueInfo with sanitized values. */ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { String name = info.name; if (info.name.length() == 0) { name = parameter.getParameterName(); if (name == null) { throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() + "] not available, and parameter name information not found in class file either."); } } String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue); return new NamedValueInfo(name, info.required, defaultValue); }
/** * Update the contextPath, servletPath, and pathInfo of the request. */ private void updatePathRequestProperties(MockHttpServletRequest request, String requestUri) { Assert.isTrue(requestUri.startsWith(this.contextPath), "requestURI [" + requestUri + "] does not start with contextPath [" + this.contextPath + "]"); request.setContextPath(this.contextPath); request.setServletPath(this.servletPath); if (ValueConstants.DEFAULT_NONE.equals(this.pathInfo)) { Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath), "Invalid servletPath [" + this.servletPath + "] for requestURI [" + requestUri + "]"); String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length()); this.pathInfo = (StringUtils.hasText(extraPath)) ? extraPath : null; } request.setPathInfo(this.pathInfo); }
protected void handleArg(MultiValueMap<String, Object> values, ApiClientMethodParameter mp, final Object pvalue, boolean flatable){ 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()); } } if(flatable){ 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(); } } if(ctx!=null){ // System.out.println("ctx.getName():"+ctx.getName()); values.add(ctx.getName(), v.toString()); }else{ values.add(k, v.toString()); } // values.put(k, v); }); }else{ values.add(mp.getParameterName(), pvalue); } }
@Override public Param build(Method javaMethod, int paramIndex, Annotation paramAnnotation) { final RequestParam requestParam = (RequestParam) paramAnnotation; final Param param = new Param() .withName(discoverParamName(javaMethod, paramIndex, requestParam.value())) .withStyle(ParamStyle.QUERY) .withRequired(requestParam.required()) .withType(grammarsDiscoverer.discoverQNameFor(new ClassMetadataFromParam(javaMethod, paramIndex))); if (!ValueConstants.DEFAULT_NONE.equals(requestParam.defaultValue())) { param.setDefault(requestParam.defaultValue()); } return param; }
public RequestParamNamedValueInfo() { super("", false, ValueConstants.DEFAULT_NONE); }
protected String parseDefaultValueAttribute(String value) { return (ValueConstants.DEFAULT_NONE.equals(value) ? null : value); }
private String getTypeWxApiHost(WxApiRequest wxApiRequest, String defaultHost) { if (wxApiRequest == null || StringUtils.isEmpty(wxApiRequest.host()) || ValueConstants.DEFAULT_NONE.equals(wxApiRequest.host())) { return defaultHost; } return wxApiRequest.host(); }
private String getTypeWxApiPropertyPrefix(WxApiRequest wxApiRequest) { if (wxApiRequest == null || StringUtils.isEmpty(wxApiRequest.prefix()) || ValueConstants.DEFAULT_NONE.equals(wxApiRequest.prefix())) { return WX_API_PROPERTY_PREFIX; } return wxApiRequest.prefix(); }
private String getTypeWxApiRequestPath(WxApiRequest wxApiRequest) { if (wxApiRequest == null || StringUtils.isEmpty(wxApiRequest.path()) || ValueConstants.DEFAULT_NONE.equals(wxApiRequest.path())) { return "/"; } return wxApiRequest.path(); }
@Override protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { @SuppressWarnings("unchecked") Map<String, MultiValueMap<String, String>> pathParameters = (Map<String, MultiValueMap<String, String>>) request.getAttribute( HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); if (CollectionUtils.isEmpty(pathParameters)) { return null; } String pathVar = parameter.getParameterAnnotation(MatrixVariable.class).pathVar(); List<String> paramValues = null; if (!pathVar.equals(ValueConstants.DEFAULT_NONE)) { if (pathParameters.containsKey(pathVar)) { paramValues = pathParameters.get(pathVar).get(name); } } else { boolean found = false; paramValues = new ArrayList<String>(); for (MultiValueMap<String, String> params : pathParameters.values()) { if (params.containsKey(name)) { if (found) { String paramType = parameter.getParameterType().getName(); throw new ServletRequestBindingException( "Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use pathVar attribute to disambiguate."); } paramValues.addAll(params.get(name)); found = true; } } } if (CollectionUtils.isEmpty(paramValues)) { return null; } else if (paramValues.size() == 1) { return paramValues.get(0); } else { return paramValues; } }
public PathVariableNamedValueInfo(PathVariable annotation) { super(annotation.value(), true, ValueConstants.DEFAULT_NONE); }
public IdPathVariableNamedValueInfo(MethodParameter parameter) { super(parameter.getParameterName(), true, ValueConstants.DEFAULT_NONE); }
protected boolean isCustomDefaultValue(final String defaultValue) { return defaultValue != null && !ValueConstants.DEFAULT_NONE.equals(defaultValue); }
private static boolean hasDefaultValue(RequestParam annot) { return !ValueConstants.DEFAULT_NONE.equals(annot.defaultValue()); }
private PathVariableNamedValueInfo(PathVariable annotation) { super(annotation.value(), true, ValueConstants.DEFAULT_NONE); }
private RequestParamNamedValueInfo() { super("", false, ValueConstants.DEFAULT_NONE); }