zambotn
zambotn

Reputation: 785

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?

Upvotes: 1

Views: 803

Answers (2)

WesternGun
WesternGun

Reputation: 12787

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.

Upvotes: 1

Matteo Aliffi
Matteo Aliffi

Reputation: 21

Quarkus supports usage of JsonViews to manage the serialization/deserialization of request/response.

(Just to let you know, sadly it's not supported (yet) by smallry-openapi implementation, so even if the serialization would work, you'll still see the full model in swagger.)

An example of usage, taken from official guide https://quarkus.io/guides/resteasy-reactive#jsonview-support:

JAX-RS methods can be annotated with @JsonView in order to customize the serialization of the returned POJO, on a per method-basis. This is best explained with an example.

A typical use of @JsonView is to hide certain fields on certain methods. In that vein, let’s define two views:

public class Views {

    public static class Public {
    }

    public static class Private extends Public {
    }
}

Let’s assume we have the User POJO on which we want to hide some field during serialization. A simple example of this is:

public class User {

    @JsonView(Views.Private.class)
    public int id;

    @JsonView(Views.Public.class)
    public String name;
}

Depending on the JAX-RS method that returns this user, we might want to exclude the id field from serialization - for example you might want an insecure method to not expose this field. The way we can achieve that in RESTEasy Reactive is shown in the following example:

@JsonView(Views.Public.class)
@GET
@Path("/public")
public User userPublic() {
    return testUser();
}

@JsonView(Views.Private.class)
@GET
@Path("/private")
public User userPrivate() {
    return testUser();
}

When the result the userPublic method is serialized, the id field will not be contained in the response as the Public view does not include it. The result of userPrivate however will include the id as expected when serialized.

Upvotes: 2

Related Questions