如何在QUARKUS Resteasy JSONB中返回具有空值的字段并覆盖排序键?

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

How to return fields with null values in QUARKUS Resteasy JSONB and override sorted keys?

问题

我使用以下库:

  1. quarkus-hibernate-orm-panache
  2. quarkus-agroal quarkus-jdbc-mysql
  3. quarkus-resteasy-jsonb
  4. quarkus-resteasy
  5. 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"}]

问题:

  1. 如果我使用 "quarkus-resteasy-jackson",它会将 null 值作为响应的一部分返回。而 "quarkus-resteasy-jsonb" 则不会返回带有 null 值的列作为响应的一部分。在 id:0b3d7518f3 中响应中没有 "description"。我需要所有字段。如何实现这一点?

  2. Jackson 的 JSON 节点顺序是 "id, name, description",与我在实体中的顺序相同。而 JsonB 中的顺序是 "description,id,name"。它使用了排序后的键。有办法在 JSON 中覆盖它吗?

谢谢。

英文:

I use the below libs

  1. quarkus-hibernate-orm-panache
  2. quarkus-agroal quarkus-jdbc-mysql
  3. quarkus-resteasy-jsonb
  4. quarkus-resteasy
  5. rest-assured

My @Entity

public class Products extends PanacheEntityBase implements Serializable{

    private static final long serialVersionUID = 2L;
    @Id
    @Column( name = &quot;id&quot; )
    public String id;
    public String name;
    public String description;
}

My Resources

@GET
    @Produces(MediaType.APPLICATION_JSON)
    public List&lt;Products&gt; getProducts() {
        return Products.listAll() ;
    }

With "quarkus-resteasy-jackson" I get

[{&quot;id&quot;:&quot;0b3d7518&quot;,&quot;name&quot;:&quot;tests org&quot;,&quot;description&quot;:null},{&quot;id&quot;:&quot;78787518f&quot;,&quot;name&quot;:&quot;ci tests org 2&quot;,&quot;description&quot;:&quot;some text&quot;}]

vs

With "quarkus-resteasy-jsonb" I get

[{&quot;id&quot;:&quot;0b3d7518f3&quot;,&quot;name&quot;:&quot;tests org&quot;},{&quot;description&quot;:&quot;some text&quot;,&quot;id&quot;:&quot;78787518f&quot;,&quot;name&quot;:&quot;ci tests org 2&quot;}]

Question ?

  1. 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. ?

  2. 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);

huangapple
  • 本文由 发表于 2020年10月9日 04:06:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64269885.html
匿名

发表评论

匿名网友

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

确定