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 27eb5a6 commit 2d03a3c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 82 deletions.
40 changes: 24 additions & 16 deletions SpringBootCustomJackson/build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example;
package com.example.java;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Model> {

@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<String> 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<Model> {

@Override
public Class<Model> 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();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.domain;
package com.example.java.domain;

public class Model {

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 2d03a3c

Please sign in to comment.