Java8 SpringBoot 2.1.9.RELEASE
--Policies to deserialize in Jackson but not use basic Setter
--When deserializing in Jackson, Getter / Setter
is unnecessary when the field visibility defined in the object is public, and it is necessary when it is protected or less, but this time it is a policy not to define it in public
――Aim to be immutable as much as possible
Send JSON: {"id ":" 1 "," name ":" Name "}
/**
*Write with Getter
*/
public class Sample {
String id;
String name;
public String getId() {
return id;
}
public String getName() {
return name;
}
}
/**
*Write in constructor
*/
public class Sample {
String id;
String name;
public Sample(String id, String name) {
this.id = id;
this.name = name;
}
}
Send JSON: {"id ": 1," age ": 20}
/**
*Write with Getter
*/
public class Sample {
int id;
int age;
public int getId() {
return id;
}
public int getAge() {
return age;
}
}
/**
*Write in constructor
*/
public class Sample {
int id;
int age;
public Sample(int id, int age) {
this.id = id;
this.age = age;
}
}
By the way, if you use Integer type, you can tolerate null (anger)
{"id": null, "age": 20}
public class Sample {
Integer id;
int age;
public Sample(Integer id, int age) {
this.id = id;
this.age = age;
}
}
Result: ʻIntSample {id = null, age = 20} `
__ Could not be implemented with Getter __
So only how to write in the constructor
Send JSON: {"isHoge ": true," isFuga ": true}
/**
*Write in constructor
*/
public class BooleanSample {
boolean isHoge;
boolean isFuga;
public BooleanSample(boolean isHoge, boolean isFuga) {
this.isHoge = isHoge;
this.isFuga = isFuga;
}
}
For Boolean type, it will be null
if deserialization is not possible
I verified it as String, int, and boolean, but it seems that the constructor is required when boolean is mixed, so I can not consider how to write with Getter
. Therefore, it seems that only the constructor can be defined.
I tried it with Getter
Send JSON: {" name ":" Kiguri "," age ": 24," isPerson ": true}
/**
*Write with Getter
*/
public class Sample {
String name;
int age;
boolean isPerson;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isPerson() {
return isPerson;
}
}
Result: Sample {name ='Kiguri', age = 24, isPerson = false}
The value of boolean is not bound, which is a very disappointing result ...
/**
*Write in constructor
*/
public class Sample {
String name;
int age;
boolean isPerson;
public Sample(String name, int age, boolean isPerson) {
this.name = name;
this.age = age;
this.isPerson = isPerson;
}
}
Result: SIBSample {name ='Kiguri', age = 24, isPerson = true}
I'm smiling.
Did you notice that the sample code so far has prepared two field definitions in common? If there is only one field definition and it is deserialized by the constructor, it is different from the original usage, but if you do not use @JsonCreator
and @JsonProperty
, you will get an exception.
Send JSON: {" name ":" Kiguri "}
public class Sample {
String name;
public Sample(String name) {
this.name = name;
}
}
{
"timestamp": "2019-10-11T03:25:50.724+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot construct instance of `sandbox.Sample` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `sandbox.Sample` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 1, column: 2]",
"path": "/s"
}
Send JSON: {" name ":" Kiguri "}
public class Sample {
String name;
@JsonCreator
public Sample(@JsonProperty("name") String name) {
this.name = name;
}
}
I wonder if it will be a title scam that completely explained Jackson.
Also, please tell me who is familiar with the trap part.
You might be asked, "Isn't it okay to add @JsonProperty
to the field without using a constructor? ", But I'll limit the instance creation method to one and add it to the constructor argument for ease of viewing. I will. Looking for a better way !! !!
Recommended Posts