――Cette fois, c'est comme le titre C'est un article relativement semblable
――Lorsque je faisais Api, je pensais qu'il n'y avait aucun moyen de supprimer chaque nom de clé de Response quand il y avait un champ avec un contenu nul, donc je vais résumer un peu ce que j'ai étudié.
HelloDto.java
public class HelloDto {
private String name;
private Integer age;
private String address;
//Obtention et constructeur omis
}
--Appeler de manière appropriée
HelloController.java
public class HelloController {
public HelloDto getHello() {
return new HelloDto("tom", 21, null);
}
}
――A ce moment, quand j'essaye d'obtenir la réponse de HelloDto, cela ressemble à ceci
hello.json
{
"name": "tom",
"age": 21,
"address": null
}
―― Dans ce cas, que faire si vous souhaitez supprimer l'adresse avec le nom de la clé? Au fait, même si vous préparez un constructeur avec des arguments différents, cela ne servait à rien car la réponse était remplie de null et n'était pas retournée.
--Résoudre à l'aide d'annotations Jackson
@JsonInclude (JsonInclude.Include.NON_NULL)
HelloDto.java
public class HelloDto {
private String name;
private Integer age;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String address;
//Obtention et constructeur omis
}
hello.json
{
"name": "tom",
"age": 21
}
@JsonInclude (JsonInclude.Include)
a NON_NULL
et NON_EMPTY
(il y en a d'autres), et quand je lis JavadocJsonInclude.java
/**
* Value that indicates that only properties with non-null
* values are to be included.
*/
NON_NULL,
/**
* Value that indicates that only properties with null value,
* or what is considered empty, are not to be included.
* Definition of emptiness is data type specific; see below
* for details on actual handling.
*<p>
* Default emptiness for all types includes:
*<ul>
* <li><code>Null</code> values.</li>
* <li>"Absent" values (see {@link #NON_ABSENT})</li>
*</ul>
* so that as baseline, "empty" set includes values that would be
* excluded by both {@link #NON_NULL} and {@link #NON_ABSENT}.
*<br>
* Beyond this base, following types have additional empty values:
*<ul>
* <li>For {@link java.util.Collection}s and {@link java.util.Map}s,
* method <code>isEmpty()</code> is called;
* </li>
* <li>For Java arrays, empty arrays are ones with length of 0
* </li>
* <li>For Java {@link java.lang.String}s, <code>length()</code> is called,
* and return value of 0 indicates empty String
* </li>
* </ul>
* and for other types, null values are excluded but other exclusions (if any).
*<p>
* Note that this default handling can be overridden by custom
* <code>JsonSerializer</code> implementation: if method <code>isEmpty()</code>
* is overridden, it will be called to see if non-null values are
* considered empty (null is always considered empty).
*<p>
* Compatibility note: Jackson 2.6 included a wider range of "empty" values than
* either earlier (up to 2.5) or later (2.7 and beyond) types; specifically:
*<ul>
* <li>Default values of primitive types (like <code>0</code> for `int`/`java.lang.Integer`
* and `false` for `bool`/`Boolean`)
* </li>
* <li>Timestamp 0 for date/time types
* </li>
*</ul>
* With 2.7, definition has been tightened back to only containing types explained
* above (null, absent, empty String, empty containers), and now
* extended definition may be specified using {@link #NON_DEFAULT}.
*/
NON_EMPTY,
――NON_EMPTY, en plus de la condition de jugement de NON_NULL, peut jouer même s'il semble être nul, et il peut également être utilisé dans des collections. Donc, sauf pour le cas où je ne veux jouer que null, je pense que NON_EMPTY va bien ... En passant, vous pouvez l'utiliser comme annotation de classe sans l'attacher à chaque champ.
HelloDto.java
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class HelloDto {
private String name;
private Integer age;
private String address;
private List<String> friends;
public HelloDto(String name, Integer age, String address, List<String> friends) {
this.name = name;
this.age = age;
this.address = address;
this.friends = friends;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getAddress() {
return address;
}
public List<String> getFriends() {
return friends;
}
}
――Appelant comme ça
HelloController.java
public class HelloController {
public HelloDto getHello() {
return new HelloDto("tom", 21, null, new ArrayList<>());
}
}
--Alors, la réponse ressemble à ceci
hello.json
{
"name": "tom",
"age": 21
}
--Il a fonctionné en toute sécurité (⌒ ▽ ⌒)
Recommended Posts