Do I need getters on JSON binding with Yasson?

225 Views Asked by At

I'm trying to avoid getters and setters on my POJO but Jersey is using my getter methods to convert my POJO to JSON.

I tried using Yasson but when I tried to remove my getters it just returns blank JSON.

// the POJO
import javax.json.bind.annotation.JsonbProperty;
public final class LoginParameter {
  @JsonbProperty("endpoint")
  private String endPoint;
  @JsonbProperty("company-id")
  private String companyId;

  public LoginParameter() {
    endPoint = "";
    companyId = "";
  }

// trying to return JSON
final LoginParameter loginInfo = new LoginParameter();
        loginInfo.setCompanyId("test");
        loginInfo.setEndPoint("endpoint!");
return Response.status(Status.OK)
    .entity(jsonb.toJson(loginInfo))
    .type(MediaType.APPLICATION_JSON_TYPE).build();
1

There are 1 best solutions below

1
Roman Grigoriadi On

By default yasson doesn't serialize private members. In order for fields to be picked up either make them public, or add custom javax.json.bind.config.PropertyVisibilityStrategy to the runtime.