简体   繁体   中英

Do I need getters on JSON binding with Yasson?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM