QUARKUS – MicroProfile REST Client: 添加一个自定义的、未映射的字段

huangapple go评论79阅读模式
英文:

QUARKUS - MicroProfile REST Client: add a custom, not mapped field

问题

我正在遵循这篇文章 https://quarkus.io/guides/rest-client 来构建一个REST客户端,以解析来自restcountries.eu服务的输出。
这里是保存模型的类:

public class Country {

    public String name;
    public String alpha2Code;
    public String capital;
    public List<Currency> currencies;

    public static class Currency {
        public String code;
        public String name;
        public String symbol;
    }
}

现在,假设我想要添加自定义字段,比如时间戳,以记录创建该对象的时刻。我想象中,我会继续添加另一个字段,如下所示:

public class Country {

    public String name;
    public String alpha2Code;
    public String capital;
    public List<Currency> currencies;
    public Instant timestamp;  //<--------- 添加的属性
    [...]

我的问题是:我如何告诉客户端填充那个字段?通常,我会在构造函数中完成。然而,我找不到解释这部分的文档。

谢谢您的帮助,

Simone

英文:

I am following this article https://quarkus.io/guides/rest-client to build a REST Client to parse the output from the restcountries.eu service.
Here the class holding the model:

public class Country {

    public String name;
    public String alpha2Code;
    public String capital;
    public List&lt;Currency&gt; currencies;

    public static class Currency {
        public String code;
        public String name;
        public String symbol;
    }
}

Now, suppose I would like to add a custom fields such as timestamp, to record the instant when this object has been created. I imagine, I would go ahead and add another field like below:

 public class Country {
    
        public String name;
        public String alpha2Code;
        public String capital;
        public List&lt;Currency&gt; currencies;
        public Instant timestamp;  //&lt;--------- added attribute
       [....]

My question is: how do I tell the client to populate that field? Normally, I would have done it in the constructor. However, I could not find docs that explain this part.

Thanks for your help

Simone

答案1

得分: 1

你实际上可以在默认构造函数中执行此操作。像 JSONB 或 Jackson 这样的框架希望POJO具有默认构造函数。在创建Country实例时,它们会调用它。

使用 @JsonbTransient@JsonIgnore 注解来标记你的POJO中的属性,以便避免解组器对在响应中找不到的属性发出投诉。

 @Data
 public class Country {
    
        private String name;
        private String alpha2Code;
        private String capital;
        private List<Currency> currencies;
        @JsonbTransient // 如果你使用的是 JSONB(在 Quarkus 中是默认的)
        @JsonIgnore // 如果你使用的是 Jackson
        private Instant timestamp;

        public Country() {
            this.timestamp = Instant.now();
        }

PS:@Data 注解是你应该考虑使用的。封装并不是一件坏事,但创建getter/setter很繁琐。然而,Project Lombok 在这方面确实很有帮助。

英文:

You can actually do this in the default constructor. Frameworks like JSONB or Jackson expect POJOs to have a default constructor. They will call it when they create an instance of Country.

Use the @JsonbTransient or @JsonIgnore annotations to mark that attribute of your POJO as ignorable in order to avoid the unmarshaller complaining about attributes that cannot be found in the response.

 @Data
 public class Country {
    
        private String name;
        private String alpha2Code;
        private String capital;
        private List&lt;Currency&gt; currencies;
        @JsonbTransient // if you&#39;re using JSONB (default in Quarkus)
        @JsonIgnore // if you&#39;re using Jackson
        private Instant timestamp;

        public Country() {
            this.timestamp = Instant.now();
        }

PS The @Data annotation is something you should consider using. Encapsulation is not a bad thing but creating getters/setters is tedious. But Project Lombok certainly helps here.

huangapple
  • 本文由 发表于 2020年10月21日 14:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定