/** * Add resolvers to support custom controller method argument types. * <p>This does not override the built-in support for resolving handler * method arguments. To customize the built-in support for argument * resolution, configure {@link RequestMappingHandlerAdapter} directly. * * @param resolvers initially an empty list */ @Override publicvoidaddArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { resolvers.add(newHandlerMethodArgumentResolver() {
/** * Whether the given {@linkplain MethodParameter method parameter} is * supported by this resolver. * * @param parameter the method parameter to check * @return {@code true} if this resolver supports the supplied parameter; * {@code false} otherwise */ @Override publicbooleansupportsParameter(MethodParameter parameter) { return parameter.getParameterType() == Page.class; }
/** * Resolves a method parameter into an argument value from a given request. * A {@link ModelAndViewContainer} provides access to the model for the * request. A {@link WebDataBinderFactory} provides a way to create * a {@link WebDataBinder} instance when needed for data binding and * type conversion purposes. * * @param parameter the method parameter to resolve. This parameter must * have previously been passed to {@link #supportsParameter} which must * have returned {@code true}. * @param mavContainer the ModelAndViewContainer for the current request * @param webRequest the current request * @param binderFactory a factory for creating {@link WebDataBinder} instances * @return the resolved argument value, or {@code null} if not resolvable * @throws Exception in case of errors with the preparation of argument values */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory)throws Exception { //获取请求对象 HttpServletRequestrequest= webRequest.getNativeRequest(HttpServletRequest.class); // 获取参数,并且转换为Map<Object, Object> Map<Object, Object> paramMap = Optional.ofNullable(request).map(HttpServletRequest::getParameterMap).map(Map::entrySet).map(Set::parallelStream).map(s -> s.collect(HashMap::new, (m, v) -> m.put(v.getKey(), Optional.ofNullable(v.getValue()).filter(ArrayUtil::isNotEmpty).map(Arrays::stream).flatMap(Stream::findFirst).orElse(null)), HashMap::putAll)).orElseGet(HashMap::new); // 处理orders Optional.ofNullable(paramMap.get(ORDERS_PROPERTY_NAME)).map(String::valueOf).map(orderString -> JSON.parseArray(orderString, OrderItem.class)).filter(CollectionUtil::isNotEmpty).ifPresent(orders -> paramMap.put(ORDERS_PROPERTY_NAME, orders)); // 序列化然后反序列化成Page对象返回 return JSON.parseObject(JSON.toJSONString(paramMap), Page.class); } }); } }