英文:
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<Currency> 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<Currency> currencies;
public Instant timestamp; //<--------- 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<Currency> currencies;
@JsonbTransient // if you're using JSONB (default in Quarkus)
@JsonIgnore // if you'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论