diff --git a/SpringBootCustomJackson/build.gradle b/SpringBootCustomJackson/build.gradle index da2e1074..01af75bc 100644 --- a/SpringBootCustomJackson/build.gradle +++ b/SpringBootCustomJackson/build.gradle @@ -1,30 +1,38 @@ -buildscript { - ext { - springBootVersion = '2.1.3.RELEASE' - } - repositories { - mavenCentral() - } - dependencies { - classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") - } +plugins { + id 'org.springframework.boot' version '2.1.3.RELEASE' + id 'org.jetbrains.kotlin.jvm' version '1.2.71' + id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71' } -apply plugin: 'java' -apply plugin: 'eclipse' -apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' -sourceCompatibility = 1.8 +sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { - compile('org.springframework.boot:spring-boot-starter-web') + implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' + implementation 'org.jetbrains.kotlin:kotlin-reflect' + implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' + + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} - testCompile('org.springframework.boot:spring-boot-starter-test') +compileKotlin { + kotlinOptions { + freeCompilerArgs = ['-Xjsr305=strict'] + jvmTarget = '1.8' + } +} + +compileTestKotlin { + kotlinOptions { + freeCompilerArgs = ['-Xjsr305=strict'] + jvmTarget = '1.8' + } } diff --git a/SpringBootCustomJackson/src/main/java/com/example/component/EncodedJsonComponent.java b/SpringBootCustomJackson/src/main/java/com/example/component/EncodedJsonComponent.java deleted file mode 100644 index 3146fff9..00000000 --- a/SpringBootCustomJackson/src/main/java/com/example/component/EncodedJsonComponent.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.example.component; - -import com.example.domain.Model; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.ObjectCodec; -import com.fasterxml.jackson.databind.*; -import org.springframework.boot.jackson.JsonComponent; -import org.springframework.util.Base64Utils; - -import java.io.IOException; -import java.util.Iterator; - -@JsonComponent -public class EncodedJsonComponent { - - public static class DecyptDataDeserializer extends JsonDeserializer { - - @Override - public Class handledType() { - return Model.class; - } - - @Override - public Model deserialize(JsonParser p, DeserializationContext ctxt) - throws IOException { - ObjectCodec codec = p.getCodec(); - JsonNode node = codec.readTree(p); - - Iterator it = node.fieldNames(); - while (it.hasNext()) { - String field = it.next(); - System.out.println(field + ":" + node.get(field)); - } - - String name = new String(Base64Utils.decodeFromString(node.get("name").asText())); - int type = node.get("type").asInt(); - System.out.println("---------------------------------------------------"); - return new Model(name, type); - } - } - - public static class EncyptDataSerializer extends JsonSerializer { - - @Override - public Class handledType() { - return Model.class; - } - - @Override - public void serialize(Model value, JsonGenerator json, - SerializerProvider provider) throws IOException { - json.writeStartObject(); - json.writeFieldName("name"); - json.writeString(Base64Utils.encodeToString(value.getName().getBytes())); - json.writeFieldName("type"); - json.writeNumber(value.getType()); - json.writeEndObject(); - } - - } -} diff --git a/SpringBootCustomJackson/src/main/java/com/example/SpringBootCustomJacksonApplication.java b/SpringBootCustomJackson/src/main/java/com/example/java/SpringBootCustomJacksonApplication.java similarity index 96% rename from SpringBootCustomJackson/src/main/java/com/example/SpringBootCustomJacksonApplication.java rename to SpringBootCustomJackson/src/main/java/com/example/java/SpringBootCustomJacksonApplication.java index aed01d01..fbcd097e 100644 --- a/SpringBootCustomJackson/src/main/java/com/example/SpringBootCustomJacksonApplication.java +++ b/SpringBootCustomJackson/src/main/java/com/example/java/SpringBootCustomJacksonApplication.java @@ -1,4 +1,4 @@ -package com.example; +package com.example.java; import java.util.Arrays; diff --git a/SpringBootCustomJackson/src/main/java/com/example/java/component/EncodedJsonComponent.java b/SpringBootCustomJackson/src/main/java/com/example/java/component/EncodedJsonComponent.java new file mode 100644 index 00000000..3ed28a8a --- /dev/null +++ b/SpringBootCustomJackson/src/main/java/com/example/java/component/EncodedJsonComponent.java @@ -0,0 +1,62 @@ +package com.example.java.component; + +import com.example.java.domain.Model; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.*; +import org.springframework.boot.jackson.JsonComponent; +import org.springframework.util.Base64Utils; + +import java.io.IOException; +import java.util.Iterator; + +@JsonComponent +public class EncodedJsonComponent { + + public static class DecyptDataDeserializer extends JsonDeserializer { + + @Override + public Class handledType() { + return Model.class; + } + + @Override + public Model deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException { + ObjectCodec codec = p.getCodec(); + JsonNode node = codec.readTree(p); + + Iterator it = node.fieldNames(); + while (it.hasNext()) { + String field = it.next(); + System.out.println(field + ":" + node.get(field)); + } + + String name = new String(Base64Utils.decodeFromString(node.get("name").asText())); + int type = node.get("type").asInt(); + System.out.println("---------------------------------------------------"); + return new Model(name, type); + } + } + + public static class EncyptDataSerializer extends JsonSerializer { + + @Override + public Class handledType() { + return Model.class; + } + + @Override + public void serialize(Model value, JsonGenerator json, + SerializerProvider provider) throws IOException { + json.writeStartObject(); + json.writeFieldName("name"); + json.writeString(Base64Utils.encodeToString(value.getName().getBytes())); + json.writeFieldName("type"); + json.writeNumber(value.getType()); + json.writeEndObject(); + } + + } +} diff --git a/SpringBootCustomJackson/src/main/java/com/example/domain/Model.java b/SpringBootCustomJackson/src/main/java/com/example/java/domain/Model.java similarity index 93% rename from SpringBootCustomJackson/src/main/java/com/example/domain/Model.java rename to SpringBootCustomJackson/src/main/java/com/example/java/domain/Model.java index fd7b1f9a..497c6894 100644 --- a/SpringBootCustomJackson/src/main/java/com/example/domain/Model.java +++ b/SpringBootCustomJackson/src/main/java/com/example/java/domain/Model.java @@ -1,4 +1,4 @@ -package com.example.domain; +package com.example.java.domain; public class Model { diff --git a/SpringBootCustomJackson/src/main/java/com/example/web/BasicController.java b/SpringBootCustomJackson/src/main/java/com/example/java/web/BasicController.java similarity index 93% rename from SpringBootCustomJackson/src/main/java/com/example/web/BasicController.java rename to SpringBootCustomJackson/src/main/java/com/example/java/web/BasicController.java index 9f294d3b..1f04cda7 100644 --- a/SpringBootCustomJackson/src/main/java/com/example/web/BasicController.java +++ b/SpringBootCustomJackson/src/main/java/com/example/java/web/BasicController.java @@ -1,6 +1,6 @@ -package com.example.web; +package com.example.java.web; -import com.example.domain.Model; +import com.example.java.domain.Model; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping;