|
@@ -1,16 +1,17 @@
|
|
package com.gxzc.zen.common.config.request
|
|
package com.gxzc.zen.common.config.request
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
|
-import com.gxzc.zen.common.config.request.annotation.ZenRequestBody
|
|
|
|
|
|
+import com.gxzc.zen.common.config.request.annotation.ZenRequestTypes
|
|
import com.gxzc.zen.common.dto.RequestDto
|
|
import com.gxzc.zen.common.dto.RequestDto
|
|
-import org.springframework.beans.BeanUtils
|
|
|
|
|
|
+import com.gxzc.zen.common.exception.ZenException
|
|
|
|
+import com.gxzc.zen.common.exception.ZenExceptionEnum
|
|
import org.springframework.core.MethodParameter
|
|
import org.springframework.core.MethodParameter
|
|
import org.springframework.http.HttpInputMessage
|
|
import org.springframework.http.HttpInputMessage
|
|
import org.springframework.http.converter.HttpMessageConverter
|
|
import org.springframework.http.converter.HttpMessageConverter
|
|
import org.springframework.web.bind.annotation.ControllerAdvice
|
|
import org.springframework.web.bind.annotation.ControllerAdvice
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter
|
|
import java.lang.reflect.Type
|
|
import java.lang.reflect.Type
|
|
-import kotlin.reflect.full.createInstance
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -20,24 +21,23 @@ import kotlin.reflect.full.createInstance
|
|
*/
|
|
*/
|
|
@ControllerAdvice
|
|
@ControllerAdvice
|
|
class ZenRequestBodyAdvice : RequestBodyAdviceAdapter() {
|
|
class ZenRequestBodyAdvice : RequestBodyAdviceAdapter() {
|
|
- override fun supports(methodParameter: MethodParameter, targetType: Type, converterType: Class<out HttpMessageConverter<*>>): Boolean {
|
|
|
|
- return methodParameter.hasParameterAnnotation(ZenRequestBody::class.java)
|
|
|
|
- }
|
|
|
|
|
|
|
|
- override fun beforeBodyRead(inputMessage: HttpInputMessage, parameter: MethodParameter, targetType: Type, converterType: Class<out HttpMessageConverter<*>>): HttpInputMessage {
|
|
|
|
- return super.beforeBodyRead(inputMessage, parameter, targetType, converterType)
|
|
|
|
|
|
+ private val mapper = ObjectMapper().apply { configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) }
|
|
|
|
+
|
|
|
|
+ override fun supports(methodParameter: MethodParameter, targetType: Type, converterType: Class<out HttpMessageConverter<*>>): Boolean {
|
|
|
|
+ return methodParameter.hasMethodAnnotation(ZenRequestTypes::class.java) && methodParameter.parameterType == RequestDto::class.java
|
|
}
|
|
}
|
|
|
|
|
|
override fun afterBodyRead(body: Any, inputMessage: HttpInputMessage, parameter: MethodParameter, targetType: Type, converterType: Class<out HttpMessageConverter<*>>): Any {
|
|
override fun afterBodyRead(body: Any, inputMessage: HttpInputMessage, parameter: MethodParameter, targetType: Type, converterType: Class<out HttpMessageConverter<*>>): Any {
|
|
- val b = body as RequestDto
|
|
|
|
- val d = b["data"]
|
|
|
|
-
|
|
|
|
- val typ = parameter.getParameterAnnotation(ZenRequestBody::class.java).value[0].value
|
|
|
|
-// val c = typ.createInstance()
|
|
|
|
-
|
|
|
|
- val a = ObjectMapper().readValue(ObjectMapper().writeValueAsString(d), typ::class.java)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ val annotation = parameter.getMethodAnnotation(ZenRequestTypes::class.java)
|
|
|
|
+ if (annotation.value.isEmpty()) {
|
|
|
|
+ return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType)
|
|
|
|
+ }
|
|
|
|
+ val typedBody = body as RequestDto
|
|
|
|
+ annotation.value.forEach {
|
|
|
|
+ val data = typedBody[it.key] ?: throw ZenException(ZenExceptionEnum.REQUEST_NULL)
|
|
|
|
+ typedBody[it.key] = mapper.convertValue(data, it.value.java)
|
|
|
|
+ }
|
|
return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType)
|
|
return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType)
|
|
}
|
|
}
|
|
|
|
|