|
@@ -1,50 +1,59 @@
|
|
|
-package com.gxzc.zen.common.config.request
|
|
|
-
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper
|
|
|
-import com.gxzc.zen.common.config.request.annotation.ZenRequestBody
|
|
|
-import org.apache.commons.io.IOUtils
|
|
|
-import org.springframework.core.MethodParameter
|
|
|
-import org.springframework.web.bind.support.WebDataBinderFactory
|
|
|
-import org.springframework.web.context.request.NativeWebRequest
|
|
|
-import org.springframework.web.method.support.HandlerMethodArgumentResolver
|
|
|
-import org.springframework.web.method.support.ModelAndViewContainer
|
|
|
-import java.io.IOException
|
|
|
-import java.nio.charset.Charset
|
|
|
-import javax.servlet.http.HttpServletRequest
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- *
|
|
|
- * @author NorthLan
|
|
|
- * @date 2018/3/16
|
|
|
- * @url https://noahlan.com
|
|
|
- */
|
|
|
-
|
|
|
-class ZenArgumentResolver : HandlerMethodArgumentResolver {
|
|
|
- init {
|
|
|
- println("init zenar")
|
|
|
- }
|
|
|
-
|
|
|
- override fun supportsParameter(parameter: MethodParameter): Boolean {
|
|
|
- return parameter.hasParameterAnnotation(ZenRequestBody::class.java)
|
|
|
- }
|
|
|
-
|
|
|
- override fun resolveArgument(parameter: MethodParameter, mavContainer: ModelAndViewContainer, webRequest: NativeWebRequest, binderFactory: WebDataBinderFactory): Any {
|
|
|
- val body = getRequestBody(webRequest)
|
|
|
- val annotation = parameter.getMethodAnnotation(ZenRequestBody::class.java)
|
|
|
-
|
|
|
- val js = ObjectMapper().readTree(body)
|
|
|
- val da = js[annotation.value[0].key]
|
|
|
-
|
|
|
- return body
|
|
|
- }
|
|
|
-
|
|
|
- private fun getRequestBody(webRequest: NativeWebRequest): String {
|
|
|
- val servletRequest = webRequest.getNativeRequest(HttpServletRequest::class.java)
|
|
|
- return try {
|
|
|
- IOUtils.toString(servletRequest.inputStream, Charset.forName("UTF-8"))
|
|
|
- } catch (e: IOException) {
|
|
|
- ""
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.gxzc.zen.common.config.request
|
|
|
+//
|
|
|
+//import com.fasterxml.jackson.databind.JsonNode
|
|
|
+//import com.fasterxml.jackson.databind.ObjectMapper
|
|
|
+//import com.gxzc.zen.common.config.request.annotation.ZenRequestTypes
|
|
|
+//import org.apache.commons.io.IOUtils
|
|
|
+//import org.springframework.core.MethodParameter
|
|
|
+//import org.springframework.web.bind.support.WebDataBinderFactory
|
|
|
+//import org.springframework.web.context.request.NativeWebRequest
|
|
|
+//import org.springframework.web.method.support.HandlerMethodArgumentResolver
|
|
|
+//import org.springframework.web.method.support.ModelAndViewContainer
|
|
|
+//import java.io.IOException
|
|
|
+//import java.nio.charset.Charset
|
|
|
+//import javax.servlet.http.HttpServletRequest
|
|
|
+//
|
|
|
+//
|
|
|
+///**
|
|
|
+// *
|
|
|
+// * @author NorthLan
|
|
|
+// * @date 2018/3/16
|
|
|
+// * @url https://noahlan.com
|
|
|
+// */
|
|
|
+//
|
|
|
+//class ZenArgumentResolver : HandlerMethodArgumentResolver {
|
|
|
+//
|
|
|
+// private val mapper = ObjectMapper()
|
|
|
+//
|
|
|
+// private val cacheNode = ThreadLocal<JsonNode>()
|
|
|
+// private val latestMethodName = ThreadLocal<String>()
|
|
|
+//
|
|
|
+// override fun supportsParameter(parameter: MethodParameter): Boolean {
|
|
|
+// if (!parameter.hasParameterAnnotation(ZenRequestTypes::class.java)) {
|
|
|
+// return false
|
|
|
+// }
|
|
|
+// val methodName = parameter.method.name
|
|
|
+// if (latestMethodName.get().isNullOrEmpty()) {
|
|
|
+// latestMethodName.set(methodName)
|
|
|
+// } else {
|
|
|
+// if (latestMethodName.get() == methodName) {
|
|
|
+// } else {
|
|
|
+// cacheNode.remove()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return true
|
|
|
+// }
|
|
|
+//
|
|
|
+// override fun resolveArgument(parameter: MethodParameter, mavContainer: ModelAndViewContainer, webRequest: NativeWebRequest, binderFactory: WebDataBinderFactory): Any {
|
|
|
+// val rootNode = try {
|
|
|
+// cacheNode.get()
|
|
|
+// ?: mapper.readTree(webRequest.getNativeRequest(HttpServletRequest::class.java).inputStream)
|
|
|
+// } catch (e: Exception) {
|
|
|
+// null
|
|
|
+// } ?: return Any()
|
|
|
+// val annotation = parameter.getParameterAnnotation(ZenRequestTypes::class.java)
|
|
|
+// val vaa = rootNode["data"].toString()
|
|
|
+// val ret = mapper.readValue(vaa, annotation.value[0].value.java)
|
|
|
+// return ret
|
|
|
+// }
|
|
|
+//}
|