-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ootCustomJackson/src/main/kotlin/com/example/kotlin/SpringBootCustomJacksonApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.kotlin | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
import java.util.* | ||
|
||
@SpringBootApplication | ||
class SpringBootCustomJacksonApplication | ||
|
||
fun main(args: Array<String>) { | ||
|
||
val context = runApplication<SpringBootCustomJacksonApplication>(*args) | ||
|
||
println("Let's inspect the beans provided by Spring Boot:") | ||
|
||
context.beanDefinitionNames.asList().sorted().forEach { println(it) } | ||
} |
56 changes: 56 additions & 0 deletions
56
SpringBootCustomJackson/src/main/kotlin/com/example/kotlin/component/EncodedJsonComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.kotlin.component | ||
|
||
import com.example.kotlin.domain.Model | ||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.* | ||
import org.springframework.boot.jackson.JsonComponent | ||
import org.springframework.util.Base64Utils | ||
import java.io.IOException | ||
|
||
@JsonComponent | ||
class EncodedJsonComponent { | ||
|
||
class DecyptDataDeserializer : JsonDeserializer<Model>() { | ||
|
||
override fun handledType(): Class<*> { | ||
return Model::class.java | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Model { | ||
val codec = p.codec | ||
val node = codec.readTree<JsonNode>(p) | ||
|
||
val it = node.fieldNames() | ||
while (it.hasNext()) { | ||
val field = it.next() | ||
println(field + ":" + node.get(field)) | ||
} | ||
|
||
val name = String(Base64Utils.decodeFromString(node.get("name").asText())) | ||
val type = node.get("type").asInt() | ||
println("---------------------------------------------------") | ||
return Model(name, type) | ||
} | ||
} | ||
|
||
class EncyptDataSerializer : JsonSerializer<Model>() { | ||
|
||
override fun handledType(): Class<Model> { | ||
return Model::class.java | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun serialize(value: Model, json: JsonGenerator, | ||
provider: SerializerProvider) { | ||
json.writeStartObject() | ||
json.writeFieldName("name") | ||
json.writeString(Base64Utils.encodeToString(value.name.toByteArray())) | ||
json.writeFieldName("type") | ||
json.writeNumber(value.type) | ||
json.writeEndObject() | ||
} | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
SpringBootCustomJackson/src/main/kotlin/com/example/kotlin/domain/Model.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.example.kotlin.domain | ||
|
||
data class Model(val name: String, val type: Int) |
39 changes: 39 additions & 0 deletions
39
SpringBootCustomJackson/src/main/kotlin/com/example/kotlin/web/BasicController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.kotlin.web | ||
|
||
import com.example.kotlin.domain.Model | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
import java.util.* | ||
|
||
@RestController | ||
class BasicController { | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(BasicController::class.java) | ||
} | ||
|
||
@GetMapping("/se") | ||
fun index(): List<Model> { | ||
val list = ArrayList<Model>() | ||
list.add(Model("abc", 1)) | ||
list.add(Model("def", 2)) | ||
list.add(Model("ghi", 3)) | ||
list.add(Model("jkl", 4)) | ||
list.add(Model("nmo", 5)) | ||
list.add(Model("pqr", 6)) | ||
list.add(Model("stu", 7)) | ||
list.add(Model("vwx", 8)) | ||
list.add(Model("yz", 9)) | ||
|
||
return list | ||
} | ||
|
||
@PostMapping("/de") | ||
fun deIndex(@RequestBody deList: List<Model>): List<Model> { | ||
logger.info(deList.toString()) | ||
return deList | ||
} | ||
} |