简体   繁体   English

如何获取所有Jackson / JAXB带注释的属性值?

[英]How to get all Jackson/JAXB annotated property values?

Given an entity (model) class with Jackson annotations for setting column names: 给定具有Jackson注释的实体(模型)类,用于设置列名:

public class GridRowModel {
    private Long maxRegulatedValue;

    @JsonProperty(value="Max Regulated Value")
    public Long getMaxRegulatedValue() {
        return maxRegulatedValue;
    }

    @JsonProperty(value="Fares")
    public Map<String, FareModel> getFares() {
        return fares;
    }
    ...

(Could alternatively have used JAXB annotations - eg @XmlElement(name="Max Regulated Value"). (也可以使用JAXB注释-例如@XmlElement(name =“ Max Regulated Value”)。

I need a getGridColumns method that will return a JSON structure with all the column names extracted from this model + its submodels (so here it would return "Max Regulated Value" and all @JsonProperty values from FareModel .) 我需要一个getGridColumns方法将返回一个JSON结构,从这个模型+其子模型提取的所有列名(所以这里将返回“最大规定值”和所有@JsonProperty从值FareModel 。)

The question is how best to approach this? 问题是如何最好地解决这个问题? Have mooted using reflection, eg something like method.getAnnotation(JsonProperty.class).value()) but not sure if this would even work and it doesn't seem ideal. 已经使用反射进行了讨论,例如method.getAnnotation(JsonProperty.class).value())但是不确定这是否还能奏效,而且看起来也不理想。 For one thing, it wouldn't respect the order specified by @JsonPropertyOrder (or propOrder in JAXB). 一方面,它不会遵守@JsonPropertyOrder (或JAXB中的propOrder )指定的顺序。 Am wondering if there is a smarter way (perhaps using some kind of Adapter?) 我想知道是否有更聪明的方法(也许使用某种适配器?)

In the end, the column order turned out to be important so I annotated each model class with: 最后,列顺序非常重要,因此我为每个模型类添加了注释:

@JsonPropertyOrder({ "Max Regulated Value", "Another field", ... }) @JsonPropertyOrder({“最大调节值”,“另一个字段”,...})

...and then wrote a method to loop through these (using reflection) + the submodel (which is actually fixed). ...然后编写了一种方法来遍历这些(使用反射)和子模型(实际上是固定的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM