Commit ceb6325e authored by liaozan's avatar liaozan 🏀

BodyParam support collections

parent f5746e09
package com.schbrain.common.web.annotation;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ValueConstants;
......@@ -7,6 +8,9 @@ import org.springframework.web.bind.annotation.ValueConstants;
import java.lang.annotation.*;
/**
* 支持从请求参数中多次获取所需参数
* {@link ObjectNode#findValue(String)}
*
* @author liaozan
* @see RequestParam
* @since 2022-12-02
......@@ -38,4 +42,4 @@ public @interface BodyParam {
*/
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
\ No newline at end of file
}
package com.schbrain.common.web.argument;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.schbrain.common.util.JacksonUtils;
......@@ -9,9 +10,11 @@ import org.springframework.core.MethodParameter;
import org.springframework.util.Assert;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver;
import org.springframework.web.util.ContentCachingRequestWrapper;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import static com.schbrain.common.web.utils.ContentCachingServletUtils.wrapRequestIfRequired;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
......@@ -50,22 +53,26 @@ public class BodyParamMethodArgumentResolver extends AbstractNamedValueMethodArg
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
JsonNode requestBody = getRequestBody(request);
JsonNode parameterValue = requestBody.get(name);
if (parameterValue == null || parameterValue.isNull()) {
JsonNode value = requestBody.findValue(name);
if (value == null || value.isNull()) {
return null;
}
Class<?> parameterType = parameter.getParameterType();
return objectMapper.convertValue(parameterValue, parameterType);
return objectMapper.convertValue(value, getJavaType(parameter));
}
private JavaType getJavaType(MethodParameter parameter) {
Type parameterType = parameter.getNestedGenericParameterType();
return objectMapper.constructType(parameterType);
}
private JsonNode getRequestBody(NativeWebRequest nativeWebRequest) throws IOException {
JsonNode requestBody = (JsonNode) nativeWebRequest.getAttribute(REQUEST_BODY_CACHE, SCOPE_REQUEST);
if (requestBody == null) {
HttpServletRequest request = wrapRequestIfRequired(nativeWebRequest.getNativeRequest(HttpServletRequest.class));
ContentCachingRequestWrapper request = wrapRequestIfRequired(nativeWebRequest.getNativeRequest(HttpServletRequest.class));
requestBody = objectMapper.readTree(request.getInputStream());
nativeWebRequest.setAttribute(REQUEST_BODY_CACHE, requestBody, SCOPE_REQUEST);
}
return requestBody;
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment