Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo Privacy Model

From Leeroopedia
Knowledge Sources
Domains Security, Data_Protection, Serialization
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for separating and protecting sensitive user data (passwords, payment codes, balance) from public profile data, provided by the APIJSON Demo's model layer.

Description

The Privacy model class demonstrates the Privacy Field Hiding pattern. It separates sensitive fields (phone, email, password, payPassword, balance) from the public User model and uses two mechanisms to prevent exposure:

  • @MethodAccess: Restricts reads to OWNER and ADMIN only (GET = {}, GETS = {OWNER, ADMIN}), while the User model allows broader access.
  • @JSONField: The get__password() and get__payPassword() methods are annotated with @JSONField(name="_password") and @JSONField(name="_payPassword"), controlling serialization output. The double-underscore getter prefix combined with the annotation ensures the field name maps correctly while the sensitive value is only accessible through authorized GETS requests.

This is a Pattern Doc — it documents how to structure models with privacy controls using annotations, not a library API.

Usage

  • Separating sensitive data: Create a separate model class for sensitive fields, distinct from the public model.
  • Hiding fields in serialization: Use @JSONField on getter methods to control field names and visibility in JSON output.
  • Restricting access: Apply @MethodAccess with GET = {} and GETS = {OWNER, ADMIN} to prevent public reads.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/model/Privacy.java:L35-124
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/model/User.java:L30-107

Signature

@MethodAccess(
    GET = {},
    GETS = {OWNER, ADMIN},
    POST = {UNKNOWN, ADMIN},
    DELETE = {ADMIN}
)
public class Privacy extends BaseModel<Long, Date> {
    private String phone;
    private String email;
    private String password;
    private String payPassword;
    private Double balance;

    @JSONField(name="_password")
    public String get__password();

    @JSONField(name="_payPassword")
    public String get__payPassword();

    public String getPhone();
    public String getEmail();
    public Double getBalance();
    // setters...
}

// Contrast with User (public data):
@MethodAccess(POST = {UNKNOWN, ADMIN}, DELETE = {ADMIN})
public class User extends BaseModel<Long, Date> implements Visitor<Long> {
    private Integer sex;
    private String head;
    private String name;
    private String tag;
    private List<String> pictureList;
    private List<Long> contactIdList;
}

Import

import apijson.demo.model.Privacy;
import apijson.demo.model.User;
import com.alibaba.fastjson2.annotation.JSONField;
import apijson.MethodAccess;

I/O Contract

Inputs

Name Type Required Description
phone String No Phone number (validated against PHONE regex)
email String No Email address (validated against EMAIL regex)
password String No Login password (hidden from serialization via @JSONField)
payPassword String No Payment password (hidden from serialization)
balance Double No Account balance

Outputs

Name Type Description
JSON response JSONObject Only accessible via GETS by OWNER/ADMIN; password fields excluded by default

Usage Examples

Creating a Privacy Record

// POST /post
{
    "Privacy": {
        "id": 82001,
        "phone": "13000000001",
        "_password": "123456"
    }
}

Reading Privacy (Owner Only)

// POST /gets  (requires OWNER or ADMIN role)
{
    "Privacy": {
        "id": 82001
    }
}
// Response includes phone, email, balance
// Password fields may be excluded based on serialization config

The @JSONField Hiding Mechanism

// The getter method name get__password() with double underscore
// combined with @JSONField(name="_password") causes fastjson to:
// 1. Map the Java field "password" to JSON key "_password"
// 2. The underscore prefix convention signals this is a hidden field
// 3. Only authorized GETS requests return this field
@JSONField(name="_password")
public String get__password() {
    return password;
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment