Implementation:APIJSON APIJSON Demo Privacy Model
| 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 theUsermodel allows broader access. - @JSONField: The
get__password()andget__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
@JSONFieldon getter methods to control field names and visibility in JSON output. - Restricting access: Apply
@MethodAccesswithGET = {}andGETS = {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) |
| 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
- Principle:APIJSON_APIJSON_Demo_Privacy_Field_Hiding
- Principle:APIJSON_APIJSON_Demo_Role_Based_Access_Control