1

我使用以下库

  1. quarkus-hibernate-orm-华丽
  2. quarkus-agroal quarkus-jdbc-mysql
  3. quarkus-resteasy-jsonb
  4. quarkus-resteasy
  5. 放心

我的@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,它会返回空值作为响应的一部分。而 quarkus-resteasy-jsonb 不返回具有空值的列作为响应的一部分。id:0b3d7518f3 的响应中没有“描述”。我需要所有领域。我怎样才能实现它。?

  2. json 节点的杰克逊顺序是我在实体中订购的方式“id、name、description”。而 JsonB 它是“描述、ID、名称”。它使用排序键。有没有办法在json中覆盖它?

谢谢

4

2 回答 2

1

好吧,我想说你自己回答了这个问题:如果杰克逊符合你的需要,那就用杰克逊。

如果你真的想使用 JSON-B,你可以配置JsonbConfig一个JsonbConfigCustomizerbean。

请参阅https://quarkus.io/guides/rest-json#json-b

您可以确定需要空值并调整排序。

于 2020-10-08T20:57:58.313 回答
1

@Guillaume Smet 上面的答案确实帮助我解决了这个问题。这是其他人想要的代码。

@Singleton
public class MyJsonbFormatConfig implements JsonbConfigCustomizer {

  public void customize(JsonbConfig config) {
        config.withNullValues(true);
   }
}

对于订购,这里是 JsonbConfig 属性。

config.withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);
于 2020-10-08T23:03:38.230 回答