Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
panwangnan
schbrain-parent
Commits
ceb6325e
Commit
ceb6325e
authored
Jul 07, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BodyParam support collections
parent
f5746e09
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
7 deletions
+18
-7
commons/web-common/src/main/java/com/schbrain/common/web/annotation/BodyParam.java
...in/java/com/schbrain/common/web/annotation/BodyParam.java
+5
-1
commons/web-common/src/main/java/com/schbrain/common/web/argument/BodyParamMethodArgumentResolver.java
.../common/web/argument/BodyParamMethodArgumentResolver.java
+13
-6
No files found.
commons/web-common/src/main/java/com/schbrain/common/web/annotation/BodyParam.java
View file @
ceb6325e
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
}
commons/web-common/src/main/java/com/schbrain/common/web/argument/BodyParamMethodArgumentResolver.java
View file @
ceb6325e
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
||
parameterV
alue
.
isNull
())
{
JsonNode
value
=
requestBody
.
findValue
(
name
);
if
(
value
==
null
||
v
alue
.
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment