英文:
How to return fields with null values in QUARKUS Resteasy JSONB and override sorted keys?
问题
我使用以下库:
- quarkus-hibernate-orm-panache
- quarkus-agroal quarkus-jdbc-mysql
- quarkus-resteasy-jsonb
- quarkus-resteasy
- rest-assured
我的 @Entity:
public class Products extends PanacheEntityBase implements Serializable {
private static final long serialVersionUID = 2L;
@Id
@Column(name = "id")
public String id;
public String name;
public String description;
}
我的资源:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Products> getProducts() {
return Products.listAll();
}
使用 "quarkus-resteasy-jackson" 我得到:
[{"id":"0b3d7518","name":"tests org","description":null},{"id":"78787518f","name":"ci tests org 2","description":"some text"}]
对比之下,
使用 "quarkus-resteasy-jsonb" 我得到:
[{"id":"0b3d7518f3","name":"tests org"},{"description":"some text","id":"78787518f","name":"ci tests org 2"}]
问题:
-
如果我使用 "quarkus-resteasy-jackson",它会将 null 值作为响应的一部分返回。而 "quarkus-resteasy-jsonb" 则不会返回带有 null 值的列作为响应的一部分。在 id:0b3d7518f3 中响应中没有 "description"。我需要所有字段。如何实现这一点?
-
Jackson 的 JSON 节点顺序是 "id, name, description",与我在实体中的顺序相同。而 JsonB 中的顺序是 "description,id,name"。它使用了排序后的键。有办法在 JSON 中覆盖它吗?
谢谢。
英文:
I use the below libs
- quarkus-hibernate-orm-panache
- quarkus-agroal quarkus-jdbc-mysql
- quarkus-resteasy-jsonb
- quarkus-resteasy
- rest-assured
My @Entity
public class Products extends PanacheEntityBase implements Serializable{
private static final long serialVersionUID = 2L;
@Id
@Column( name = "id" )
public String id;
public String name;
public String description;
}
My Resources
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Products> getProducts() {
return Products.listAll() ;
}
With "quarkus-resteasy-jackson" I get
[{"id":"0b3d7518","name":"tests org","description":null},{"id":"78787518f","name":"ci tests org 2","description":"some text"}]
vs
With "quarkus-resteasy-jsonb" I get
[{"id":"0b3d7518f3","name":"tests org"},{"description":"some text","id":"78787518f","name":"ci tests org 2"}]
Question ?
-
If I use, quarkus-resteasy-jackson, it returns null value as a part of response. while quarkus-resteasy-jsonb does not return columns with null value as a part of response. "description" is not there in the response for id:0b3d7518f3. I need all fields. How can I achieve it. ?
-
Jackson order of json nodes is "id, name, description" the way I ordered in Entity. While JsonB it is "description,id,name". It is using sorted keys. Is there a way to override it in json?
Thanks
答案1
得分: 1
好的,以下是翻译好的内容:
嗯,我想说你已经自己回答了这个问题:如果Jackson符合你的需求,就使用Jackson。
如果你真的想要使用JSON-B,你可以使用一个JsonbConfigCustomizer
bean 来配置JsonbConfig
。
请参考 https://quarkus.io/guides/rest-json#json-b 。
你当然可以要求包括空值,并且也可以调整排序。
英文:
Well, I would say you answered the question yourself: if Jackson fits your needs, just use Jackson.
If you really want to use JSON-B, you can configure JsonbConfig
with a JsonbConfigCustomizer
bean.
See https://quarkus.io/guides/rest-json#json-b .
You can require the null values for sure and also tweak the ordering.
答案2
得分: 1
@Guillaume Smet上面的答案确实帮助我解决了问题。以下是代码,以防其他人也在寻找解决方法:
@Singleton
public class MyJsonbFormatConfig implements JsonbConfigCustomizer {
public void customize(JsonbConfig config) {
config.withNullValues(true);
}
}
关于排序,这是JsonbConfig属性。
config.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);
英文:
@Guillaume Smet above answer did help me solve it. Here is the code in case others are looking to..
@Singleton
public class MyJsonbFormatConfig implements JsonbConfigCustomizer {
public void customize(JsonbConfig config) {
config.withNullValues(true);
}
}
For ordering, here is the JsonbConfig property.
config.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论