Skip to content

Commit

Permalink
[FIX] #26 SpringBootCustomJackson
Browse files Browse the repository at this point in the history
  • Loading branch information
heowc committed Apr 6, 2019
1 parent 2d03a3c commit f9fe79e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public static void main(String[] args) {

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
Arrays.stream(ctx.getBeanDefinitionNames()).sorted().forEach(System.out::println);
}
}
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) }
}
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()
}

}
}
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)
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
}
}

0 comments on commit f9fe79e

Please sign in to comment.