2
2
.
.
1
1
1
1
.
.
1
1
1
1
F
F
r
r
o
o
m
m
J
J
S
S
O
O
N
N
-
-
@
@
J
J
s
s
o
o
n
n
D
D
e
e
s
s
e
e
r
r
i
i
a
a
l
l
i
i
z
z
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to create custom Deserializer to convert JSON HTTP Request into DTO in Controller.
Deserializer is used to (default JSON Deserializer can't perform this conversion)
convert JSON Property height "1,67" (String which uses comma and can't be converted into Float)
into DTO Property height "1.67" (String which uses dot and can be converted into Float)
Deserializer is
implemented as class PersonDTODeserializer extends JsonDeserializer<PersonDTO>
attached to PersonDTO with @JsonDeserialize(using = PersonDTODeserializer.class)
called as Controller Input addPerson(@RequestBody PersonDTO personDTO)
Syntax
@JsonDeserialize(using = PersonDTODeserializer.class)
public class PersonDTO { ... }
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: @Controller, @RequestMapping, Tomcat Server
MyController
PersonDTO
POSTMAN
PersonDTO
Deserializer
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_json_deserializer (add Spring Boot Starters from the table)
Create Package: DTO (inside main package)
Create Class: PersonDTODeserializer.java (inside package DTO)
Create Class: PersonDTO.java (inside package DTO)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
PersonDTODeserializer.java
package com.ivoronline.springboot_json_deserializer.DTO;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
public class PersonDTODeserializer extends JsonDeserializer<PersonDTO> {
@Override
public PersonDTO deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
//PREPARE JSON STUFF
ObjectCodec objectCodec = jsonParser.getCodec();
JsonNode node = objectCodec.readTree(jsonParser);
//DESERIALIZE PROBLEMATIC JSON PROPERTY: HEIGHT
String heightString = node.get("height").asText();
heightString = heightString.replace(',', '.'); //Convert "1,67" into "1.67"
Float height = Float.parseFloat(heightString); //Create Float from String
//CREATE PERSONDTO
PersonDTO personDTO = new PersonDTO();
personDTO.name = node.get("name").asText(); //NORMAL PROPERTY
personDTO.height = height; //PROBLEMATIC PROPERTY
//RETURN PERSONDTO
return personDTO;
}
}
PersonDTO.java
package com.ivoronline.springboot_json_deserializer.DTO;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(using = PersonDTODeserializer.class)
public class PersonDTO {
public String name;
public Float height;
}
MyController.java
package com.ivoronline.springboot_json_deserializer.controllers;
import com.ivoronline.springboot_json_deserializer.DTO.PersonDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson(@RequestBody PersonDTO personDTO) {
//GET DATA FROM PersonDTO
String name = personDTO.name;
Float height = personDTO.height;
//RETURN SOMETHING
return name + " is " + height + " meters high";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
Start Postman
POST
http://localhost:8080/AddPerson
Headers (add Key-Value)
Content-Type: application/json
Body (option: raw)
{
"name" : "John",
"height" : "1,67"
}
Postman
HTTP Response Body
John is 1.67 meters high