Simplify SpringBoot ID, Enum, and Dictionary Translation with a Single @Trans Annotation

Easy‑Trans is a SpringBoot starter that eliminates repetitive ID‑to‑name, dictionary code‑to‑text, and cross‑service field translation code by using a single @Trans annotation, offering five translation modes, Maven integration, YAML configuration, and optional Redis caching to boost performance and reduce boilerplate.

IoT Full-Stack Technology
IoT Full-Stack Technology
IoT Full-Stack Technology
Simplify SpringBoot ID, Enum, and Dictionary Translation with a Single @Trans Annotation

Overview

Easy-Trans is a SpringBoot starter that centralizes data‑translation tasks (e.g., converting user IDs to names, mapping dictionary codes to readable text, and cross‑service field translation) through a single @Trans annotation, reducing repetitive business code.

Translation scenarios addressed

Automatically fetch user name and phone number from a user ID without manual joins.

Convert binary dictionary codes (0/1) to human‑readable values ("男"/"女").

Batch‑translate multiple user IDs into a comma‑separated list such as "张三,李四,王五".

Map enum values directly to front‑end display text.

One‑click translation of IDs across micro‑services without writing RPC/Feign code.

Built‑in translation types

Easy‑Trans defines five translation modes, selected via the type attribute of @Trans:

SIMPLE – ID → name/phone from a target entity.

DICTIONARY – Code → text using a dictionary cache.

ENUM – Enum value → display text.

RPC – Cross‑service translation via RPC.

AUTO_TRANS – Custom data source with user‑defined query logic.

Integration steps

1. Maven dependencies

<!-- Core starter -->
<dependency>
    <groupId>org.dromara</groupId>
    <artifactId>easy-trans-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>

<!-- MyBatis‑Plus extension (use jpa‑extend for JPA) -->
<dependency>
    <groupId>org.dromara</groupId>
    <artifactId>easy-trans-mybatis-plus-extend</artifactId>
    <version>2.3.1</version>
</dependency>

2. Configuration (application.yml)

easy-trans:
  is-enable-redis: true          # enable Redis cache
  is-enable-global: true           # intercept all response bodies for automatic translation
  is-enable-tile: false           # flat mode: translated fields appear at the same level as original fields
  dict-use-redis: true            # store dictionary cache in Redis for micro‑service mode
  is-enable-custom-rpc: true       # enable @RpcTrans for RPC‑based translation
  is-enable-map-result: true      # enable map‑result handling for Ruoyi framework
  db-type: mysql                  # reverse‑translation database type
  mp-new: true                    # enable for MyBatis‑Plus 3.5.3.2+ versions

3. Entity definition

@Data
public class User implements TransPojo {
    private Long id;
    private String userName;
    private Integer gender;
}

Translation mode examples

SIMPLE (ID → name/phone)

@Data
public class Device implements TransPojo {
    private Long id;
    private String deviceName;
    @Trans(
        type = TransType.SIMPLE,
        target = User.class,
        fields = {"userName", "phone"}
    )
    private Long userId;
}

After querying, translated values are placed in the transMap field and returned to the front‑end.

DICTIONARY (code → text)

@Autowired
private DictionaryTransService dictService;

Map<String, String> map = new HashMap<>();
map.put("0", "男");
map.put("1", "女");

dictService.refreshCache("gender", map);
@Trans(type = TransType.DICTIONARY, key = "gender")
private Integer gender; // 性别

ENUM (enum → display)

public enum SexEnum {
    MAN(1, "男"),
    WOMAN(0, "女");
    private Integer code;
    private String desc;
    // constructor, getters omitted
}
@Trans(type = TransType.ENUM, key = "desc")
private SexEnum sexEnum;

RPC (cross‑service translation)

@Trans(
    type = TransType.RPC,
    serviceName = "user-service",
    targetClassName = "cn.demo.user.pojo.User",
    fields = "userName"
)
private Long userId;

AUTO_TRANS (custom data source)

easy-trans:
  autotrans:
    package: com.demo.service.**
@AutoTrans(namespace = "user", fields = {"userName"})
@Service
public class UserServiceImpl implements AutoTransable<User> {
    @Override
    public List<User> selectByIds(List<?> ids) {
        // custom query logic
        return userMapper.selectBatchIds(ids);
    }
}

Cache optimization

@TransDefaultSett(
    isUseCache = true,
    cacheSeconds = 600,
    maxCache = 1000
)
public class User implements TransPojo { ... }

With caching enabled, only the primary table is queried; associated data is served from Redis, dramatically improving API response times.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

microservicesspringbootAnnotationData TranslationEasy-TransRedis CacheEnum Mapping
IoT Full-Stack Technology
Written by

IoT Full-Stack Technology

Dedicated to sharing IoT cloud services, embedded systems, and mobile client technology, with no spam ads.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.