Javadoc can be as clear and readable as "@JsonUnwrapped"
🍀 🍀 The javadoc screenshot at first.

🍀 🍀 Let's take a glance at its javadoc bellow.
package: com.fasterxml.jackson.annotation
@Target({ElementType.ANNOTATION_TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonUnwrapped
extends java.lang.annotation.Annotation
Annotation used to indicate that a property should be serialized "unwrapped"; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.
For example, consider case of POJO like:
public class Parent {
public int age;
public Name name;
}
public class Name {
public String first, last;
}
which would normally be serialized as follows (assuming @JsonUnwrapped had no effect):
{
"age" : 18,
"name" : {
"first" : "Joey",
"last" : "Sixpack"
}
}
can be changed to this:
{
"age" : 18,
"first" : "Joey",
"last" : "Sixpack"
}
by changing Parent class to:
public class Parent {
public int age;
@JsonUnwrapped
public Name name;
}
Annotation can only be added to properties, and not classes, as it is contextual.
Also note that annotation only applies if
- Value is serialized as JSON Object (can not unwrap JSON arrays using this mechanism)
- Serialization is done using
BeanSerializer, not a custom serializer - No type information is added; if type information needs to be added, structure can not be altered regardless of inclusion strategy; so annotation is basically ignored.
Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.0 (jackson-annotations-2.11.0-sources.jar)
🍀 🍀 the end.
编写清晰易读的Javadoc是每个Java开发者应该具备的良好习惯,清晰易读的Javadoc对于代码维护和团队协作至关重要。以下是详细的好处:
- 提高代码可维护性:清晰的文档帮助开发者理解代码的意图和功能,减少维护成本。
- 促进团队协作:新成员或团队成员可以快速了解API的使用方法,减少沟通成本。
- 便于API使用:其他开发者在使用你的类或方法时,不需要深入阅读实现代码,只需查看Javadoc文档即可。
- 集成开发环境(IDE)支持:在IDE中编写代码时,良好的Javadoc会以提示形式出现,帮助开发者正确使用方法。
- 自动化生成文档:Javadoc可以自动生成HTML格式的文档,方便在线浏览和搜索。
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/18991457
浙公网安备 33010602011771号