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

3.5k views Asked by At

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 = "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 ?

  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

2

There are 2 answers

0
Guillaume Smet On BEST ANSWER

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.

0
Narayanan On

@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);