Sfoglia il codice sorgente

修复Long到前端丢失精度的问题

NorthLan 6 anni fa
parent
commit
65f8ad2aed

+ 6 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/base/BaseModel.kt

@@ -7,6 +7,10 @@ import com.baomidou.mybatisplus.enums.FieldFill
 import com.baomidou.mybatisplus.enums.IdType
 import com.fasterxml.jackson.annotation.JsonFormat
 import com.fasterxml.jackson.annotation.JsonInclude
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize
+import com.fasterxml.jackson.databind.annotation.JsonSerialize
+import com.gxzc.zen.common.config.jackson.LongJsonDeserializer
+import com.gxzc.zen.common.config.jackson.LongJsonSerializer
 import io.swagger.annotations.ApiModelProperty
 import java.io.Serializable
 import java.util.*
@@ -23,6 +27,8 @@ open class BaseModel : Serializable {
 
     // 主键
     @TableId(type = IdType.AUTO)
+    @JsonSerialize(using = LongJsonSerializer::class)
+    @JsonDeserialize(using = LongJsonDeserializer::class)
     var id: Long? = null
 
     // 创建时间

+ 28 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/config/jackson/LongJsonDeserializer.kt

@@ -0,0 +1,28 @@
+package com.gxzc.zen.common.config.jackson
+
+import com.fasterxml.jackson.core.JsonParser
+import com.fasterxml.jackson.databind.DeserializationContext
+import com.fasterxml.jackson.databind.JsonDeserializer
+import org.slf4j.LoggerFactory
+
+/**
+ * String -> Long
+ * @author NorthLan
+ * @date 2018/5/15
+ * @url https://noahlan.com
+ */
+class LongJsonDeserializer : JsonDeserializer<Long>() {
+    companion object {
+        private val logger = LoggerFactory.getLogger(LongJsonDeserializer::class.java)
+    }
+
+    override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Long? {
+        val value = p?.text
+        return try {
+            value?.toLong()
+        } catch (e: NumberFormatException) {
+            logger.error("解析长整形错误", e)
+            null
+        }
+    }
+}

+ 20 - 0
zen-core/src/main/kotlin/com/gxzc/zen/common/config/jackson/LongJsonSerializer.kt

@@ -0,0 +1,20 @@
+package com.gxzc.zen.common.config.jackson
+
+import com.fasterxml.jackson.core.JsonGenerator
+import com.fasterxml.jackson.databind.JsonSerializer
+import com.fasterxml.jackson.databind.SerializerProvider
+
+/**
+ * Long转String
+ * @author NorthLan
+ * @date 2018/5/15
+ * @url https://noahlan.com
+ */
+class LongJsonSerializer : JsonSerializer<Long>() {
+    override fun serialize(value: Long?, gen: JsonGenerator?, serializers: SerializerProvider?) {
+        val text = value?.toString()
+        if (text != null) {
+            gen?.writeString(text)
+        }
+    }
+}

+ 7 - 0
zen-web/src/main/kotlin/com/gxzc/zen/web/sys/controller/TestController.kt

@@ -2,6 +2,7 @@ package com.gxzc.zen.web.sys.controller
 
 import com.gxzc.zen.common.base.BaseController
 import org.apache.shiro.SecurityUtils
+import org.springframework.http.ResponseEntity
 import org.springframework.web.bind.annotation.GetMapping
 import org.springframework.web.bind.annotation.RequestMapping
 import org.springframework.web.bind.annotation.RestController
@@ -33,4 +34,10 @@ class TestController : BaseController() {
     fun rpc() {
 //        println(testRpcService.sayHello("2333"))
     }
+
+    @GetMapping("/long")
+    fun longlonglong(): ResponseEntity<*> {
+        // 测试long
+        return ResponseEntity.ok(4567890456789045678L)
+    }
 }