登录
首页 >  文章 >  java教程

在自定义注释中使用 HashMap

时间:2025-01-15 15:50:54 228浏览 收藏

在文章实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《在自定义注释中使用 HashMap》,聊聊,希望可以帮助到正在努力赚钱的你。

在自定义注释中使用 HashMap

引言

在之前的文章“创建自定义 Jackson JsonSerializer 和 JsonDeserializer 用于映射值”中,我创建了自定义注解 @mappingtable 用于键值对映射。键值对在 JSON 中定义,并在 @mappingtable 注解中以字符串形式指定。MappingTableMapReader 类将 JSON 转换为 HashMap,并在 JsonSerializer 和 JsonDeserializer 中使用。

public class MappingTableMapReader {

    private final BeanProperty property;

    public MappingTableMapReader(BeanProperty property) {
        this.property = property;
    }

    public Map getMap() {

        mappingtable annotation = property.getAnnotation(mappingtable.class);

        if (annotation == null) {
            throw new MappingTableRuntimeException("annotation @mappingtable not set at property");
        }

        return annotation.map().getMap();
    }
}

使用枚举的完整示例: https://github.com/alaugks/article-jackson-serializer/tree/mapping-table/enum

类示例,嵌套注解示例以及结论部分与原文一致,此处省略。

以上就是《在自定义注释中使用 HashMap》的详细内容,更多关于的资料请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>