Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d03a3c

Browse files
committedApr 6, 2019
[FIX] #26 SpringBootCustomJackson
1 parent 27eb5a6 commit 2d03a3c

File tree

6 files changed

+90
-82
lines changed

6 files changed

+90
-82
lines changed
 

‎SpringBootCustomJackson/build.gradle

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1-
buildscript {
2-
ext {
3-
springBootVersion = '2.1.3.RELEASE'
4-
}
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10-
}
1+
plugins {
2+
id 'org.springframework.boot' version '2.1.3.RELEASE'
3+
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
4+
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
115
}
126

13-
apply plugin: 'java'
14-
apply plugin: 'eclipse'
15-
apply plugin: 'org.springframework.boot'
167
apply plugin: 'io.spring.dependency-management'
178

189
group = 'com.example'
1910
version = '0.0.1-SNAPSHOT'
20-
sourceCompatibility = 1.8
11+
sourceCompatibility = '1.8'
2112

2213
repositories {
2314
mavenCentral()
2415
}
2516

2617
dependencies {
27-
compile('org.springframework.boot:spring-boot-starter-web')
18+
implementation 'org.springframework.boot:spring-boot-starter-web'
19+
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
20+
implementation 'org.jetbrains.kotlin:kotlin-reflect'
21+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
22+
23+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
24+
}
2825

29-
testCompile('org.springframework.boot:spring-boot-starter-test')
26+
compileKotlin {
27+
kotlinOptions {
28+
freeCompilerArgs = ['-Xjsr305=strict']
29+
jvmTarget = '1.8'
30+
}
31+
}
32+
33+
compileTestKotlin {
34+
kotlinOptions {
35+
freeCompilerArgs = ['-Xjsr305=strict']
36+
jvmTarget = '1.8'
37+
}
3038
}

‎SpringBootCustomJackson/src/main/java/com/example/component/EncodedJsonComponent.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

‎SpringBootCustomJackson/src/main/java/com/example/SpringBootCustomJacksonApplication.java renamed to ‎SpringBootCustomJackson/src/main/java/com/example/java/SpringBootCustomJacksonApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example;
1+
package com.example.java;
22

33
import java.util.Arrays;
44

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example.java.component;
2+
3+
import com.example.java.domain.Model;
4+
import com.fasterxml.jackson.core.JsonGenerator;
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.ObjectCodec;
7+
import com.fasterxml.jackson.databind.*;
8+
import org.springframework.boot.jackson.JsonComponent;
9+
import org.springframework.util.Base64Utils;
10+
11+
import java.io.IOException;
12+
import java.util.Iterator;
13+
14+
@JsonComponent
15+
public class EncodedJsonComponent {
16+
17+
public static class DecyptDataDeserializer extends JsonDeserializer<Model> {
18+
19+
@Override
20+
public Class<?> handledType() {
21+
return Model.class;
22+
}
23+
24+
@Override
25+
public Model deserialize(JsonParser p, DeserializationContext ctxt)
26+
throws IOException {
27+
ObjectCodec codec = p.getCodec();
28+
JsonNode node = codec.readTree(p);
29+
30+
Iterator<String> it = node.fieldNames();
31+
while (it.hasNext()) {
32+
String field = it.next();
33+
System.out.println(field + ":" + node.get(field));
34+
}
35+
36+
String name = new String(Base64Utils.decodeFromString(node.get("name").asText()));
37+
int type = node.get("type").asInt();
38+
System.out.println("---------------------------------------------------");
39+
return new Model(name, type);
40+
}
41+
}
42+
43+
public static class EncyptDataSerializer extends JsonSerializer<Model> {
44+
45+
@Override
46+
public Class<Model> handledType() {
47+
return Model.class;
48+
}
49+
50+
@Override
51+
public void serialize(Model value, JsonGenerator json,
52+
SerializerProvider provider) throws IOException {
53+
json.writeStartObject();
54+
json.writeFieldName("name");
55+
json.writeString(Base64Utils.encodeToString(value.getName().getBytes()));
56+
json.writeFieldName("type");
57+
json.writeNumber(value.getType());
58+
json.writeEndObject();
59+
}
60+
61+
}
62+
}

‎SpringBootCustomJackson/src/main/java/com/example/domain/Model.java renamed to ‎SpringBootCustomJackson/src/main/java/com/example/java/domain/Model.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.domain;
1+
package com.example.java.domain;
22

33
public class Model {
44

‎SpringBootCustomJackson/src/main/java/com/example/web/BasicController.java renamed to ‎SpringBootCustomJackson/src/main/java/com/example/java/web/BasicController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.example.web;
1+
package com.example.java.web;
22

3-
import com.example.domain.Model;
3+
import com.example.java.domain.Model;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.web.bind.annotation.GetMapping;

0 commit comments

Comments
 (0)
Please sign in to comment.