Skip to content

YApi Mock

EasyYapi supports syncing field mock rules to the YApi Mock service for frontend development.

Usage

Custom Mock Values

Use the @mock tag in Javadoc:

java
public class User {
    /**
     * Username
     * @mock admin
     */
    private String name;

    /**
     * Email
     * @mock user@example.com
     */
    private String email;

    /**
     * Age
     * @mock 25
     */
    private Integer age;
}

Built-in Mock Rules

EasyYapi provides built-in mock rules based on field names and types. These rules are automatically applied when exporting to YApi.

Contact & Identity

Field PatternMock ValueDescription
*.email@emailRandom email
*.phone@phoneRandom phone
*.mobile@phoneRandom phone
*.uuid@uuidRandom UUID

ID Fields

Field PatternMock ValueDescription
*.id (integer)@integerRandom integer
*.id (long)@integerRandom integer
*.id (string)@idRandom ID string

Name & Personal

Field PatternMock ValueDescription
*.name@stringRandom string
*.age (integer)@integer(0, 120)Age between 0-120

Location

Field PatternMock ValueDescription
*.address@county(true)Full address
*.city@cityRandom city
*.country@countyRandom county
*.province@provinceRandom province
*.zip@zipRandom zip code
*.ip@ipRandom IP address

URL & Media

Field PatternMock ValueDescription
*.url@urlRandom URL
*.link@urlRandom URL
*.image@imageRandom image URL
*.avatar@imageRandom image URL

Numbers & Money

Field PatternMock ValueDescription
*.price (double/float)@float(0, 10000, 2, 2)Price with 2 decimals
*.amount (double/float)@float(0, 10000, 2, 2)Amount with 2 decimals
*.count (integer)@integer(0, 100)Count 0-100
*.page (integer)@integer(1, 100)Page number 1-100
*.size (integer)@integer(1, 100)Page size 1-100

Security

Field PatternMock ValueDescription
*.password******Masked password
*.token@string(32)32-char token

Date & Time

Field PatternMock ValueDescription
*.date@dateRandom date
*.datetime@datetimeRandom datetime

Additional Mock Heuristics

Besides the built-in rules, EasyYapi also applies intelligent heuristics based on field names:

Name ContainsMock ValueDescription
tel, mobile@phonePhone number
website@urlWebsite URL
guid@uuidUUID/GUID
user, first, last (with name)@cnameChinese name
state@provinceProvince/State
postal@zipPostal code
birth (with date)@date('yyyy-MM-dd')Birth date
money@float(0, 10000, 2, 2)Money amount
num, number, qty, quantity@integer(0, 100)Number
pwd******Masked password
key@string(32)API key
img, photo@imageImage URL
desc, description, content, remark, comment@cparagraphChinese paragraph
title@ctitleChinese title
code@string(6)6-char code

Type-based Mock

When no name-based rule matches, EasyYapi falls back to type-based mock:

TypeMock ValueDescription
String@stringRandom string
Short@integer(0, 32767)Random short
Integer@integerRandom integer
Long@integerRandom integer
Float@floatRandom float
Double@floatRandom float
Boolean@booleanRandom boolean
Array@arrayRandom array
Object@objectRandom object
File@fileFile placeholder
Date@dateRandom date
DateTime@datetimeRandom datetime

Custom Mock Rules

You can add custom mock rules in your config file:

properties
# Pattern: mock.rule=fieldName|type=mockValue
mock.rule=*.orderId|string=@id
mock.rule=*.status|integer=@integer(0, 5)

The pattern format is:

  • fieldName - Field name pattern (supports * wildcard)
  • type - JSON type (string, integer, long, double, float, boolean, etc.)
  • mockValue - Mock.js expression

Released under the Apache-2.0 License.