在 Kotlin 中与 @RequestParam 一起使用时,自定义 HashMap 类获取参数类型不匹配
Posted
技术标签:
【中文标题】在 Kotlin 中与 @RequestParam 一起使用时,自定义 HashMap 类获取参数类型不匹配【英文标题】:Custom HashMap class get argument type mismatch when using with @RequestParam in Kotlin 【发布时间】:2020-01-18 14:18:19 【问题描述】:我正在尝试编写一个简单的路由来处理带有一些查询参数的 GET 请求。为了实现这一点,我创建了自己的 Customs HashMap 类来加快一些流程并轻松扩展我需要的一些功能,如下所示:
class MMap : HashMap<String, Any>()
fun getString(key: String): String? = get(key) as? String
...
然后,在我的控制器中,我按照以下代码完成了所有操作:
@GetMapping("/user")
fun find(@RequestParam data: MMap): MMap
return data
之后,我尝试使用 GET localhost:8080/user?id=123&username=admin 对此路由进行获取请求,但突然在调试控制台中出现了一些错误: p>
Method [public com.cc.MMap com.cc.controller.UserController.find(com.cc.MMap)] with argument values:
[0] [type=java.util.LinkedHashMap] [value=id=123,username=admin] ] with root cause
java.lang.IllegalArgumentException: argument type mismatch
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:887) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.2.0.RC1.jar:5.2.0.RC1]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-5.2.0.RC1.jar:5.2.0.RC1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]
我已尝试更改:
fun find(@RequestParam data: MMap)
到 fun find(@RequestParam data: HashMap<String, Object>)
效果很好,但这不是我真正想要使用的,因为我在 MMap 中有一些快捷方式,所以除非我别无选择,否则这对我来说不是一个好选择。
class MMap : HashMap<String, Any>()
到 class MMap : HashMap<String, Object>()
但没有任何效果。
所以,如果有人可以提供帮助,请分享一些想法。谢谢。
【问题讨论】:
我猜,那个 spring 正在将查询参数放入 LinkedHashMap,这是一个 Map 但没有 MMap ...这就是为什么 map 可以工作而 MMap 不能...让你的 MMap 作为包装 Map 或使用 kotlin 扩展函数来添加您的 Map 函数? 嗨@IEE1394,我曾经尝试从LinkedHashMap 扩展MMap,但它仍然抛出相同的异常。对于 Kotlin 扩展功能,太糟糕了,我忘记了这个有用的功能。所以我会试一试。对于这句话,“MMap as a wrapper around a Map”,我不确定它是什么,所以请给出一些解释或示例。 【参考方案1】:您可以使用extension functions 和reified type parameters 执行以下操作:
inline fun <reified T> Map<String, Any>.get(key: String) = get(key) as? T
然后您可以像这样在任何地图上调用该函数:
@GetMapping("/user")
fun find(@RequestParam data: Map<String, Any>): Map<String, Any>
val id = data.get<String>("id") // string
println(id)
return data
或者非泛型扩展函数也可以工作(这可能是更好的方法,因为the parameter map will always be of type Map<String, String>
and will not automatically convert numbers to integers):
fun Map<String, Any>.getString(key: String) = get(key) as? String
fun Map<String, Any>.getInt(key: String) = getString(key)?.toInt()
【讨论】:
哇,这太酷了@Jordie。现在我已经做到了:``` typealias MMap = HashMap以上是关于在 Kotlin 中与 @RequestParam 一起使用时,自定义 HashMap 类获取参数类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章
Mockito.anyString()在Kotlin中与NPE崩溃
Kotlin中与Java互操作与可空性类型映射属性访问@JvmOverloads@JvmField@JvmStatic@Throws和函数类型操作详解