javaspringjacksonquarkusjsonb-api

Is there an equivalent of Jackson + Spring's `@JsonView` using Quarkus + JSONB?


I'm playing with Quarkus and trying to build a CRUD REST application; I'm trying to get 2 endpoints returning 2 different views of the same entities. Here is an example on how I would have done in Spring + Jackson:

@Entity
public class Car{
  public String model;
  @ManyToOne( fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
  public Owner owner;
  // [...]
}

@Entity
public class Owner{
  public String name;
  // [...]
}

Here it is the important part: now if I were using Jackson I would have create a CarView class:

public class CarView {
  public static class Public {};
  public static class Private extends Public {};
}

And with that I would have annotated Car.model with @JsonView(CarView.Public.class) and Car.owner with @JsonView(CarView.Private.class) and then just annotate with the same annotations my methods in the REST controller in order to tell Jackson which view I want to use:

@RequestMapping("/car/{id}")
@JsonView(CarView.Public.class)
public Car getPublic(@PathVariable int id) { /*...*/ }

@RequestMapping("/car/private/{id}")
@JsonView(CarView.Private.class)
public Car getPrivate(@PathVariable int id) { /*...*/ }

Can I accomplish the same result using Quarkus & JSON-B?


Solution

  • Have you checked @JsonbVisibility or "Jsonb adapter" part in https://javaee.github.io/jsonb-spec/users-guide.html annotation from Jsonb? I am afraid maybe there isn't a solution in Jsonb yet like @JsonView in Jackson. Jsonb adapter is configuration at bean level(you choose the Jsonb instance when you (de)serialize), not at view level.