One Annotation Eliminates Redundant ID‑to‑Name, Dictionary, and Enum Translations in SpringBoot

Easy‑Trans is a SpringBoot starter that provides five translation modes—simple ID‑to‑name, dictionary code‑to‑text, enum mapping, RPC cross‑service, and custom data source—allowing developers to replace repetitive lookup code with a single @Trans annotation, optionally enabled with caching for high performance.

IoT Full-Stack Technology
IoT Full-Stack Technology
IoT Full-Stack Technology
One Annotation Eliminates Redundant ID‑to‑Name, Dictionary, and Enum Translations in SpringBoot

Translation Modes

SIMPLE – translate an ID to a name (e.g., userId → userName, phone).

DICTIONARY – translate a dictionary code to text (e.g., 0/1 → "男"/"女").

ENUM – map an enum value to a display string.

RPC – cross‑service lookup without writing Feign/RestTemplate code.

AUTO_TRANS – custom data‑source translation defined by a service implementation.

Integration Steps

Add Maven dependencies:

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

<dependency>
    <groupId>org.dromara</groupId>
    <artifactId>easy-trans-mybatis-plus-extend</artifactId>
    <version>2.3.1</version>
</dependency>

Configure in application.yml:

easy-trans:
  is-enable-redis: true          # enable Redis cache
  is-enable-global: true         # intercept all response bodies
  is-enable-tile: false          # flat output mode
  dict-use-redis: true           # store dictionary cache in Redis
  is-enable-custom-rpc: true     # enable RPC translation
  is-enable-map-result: true      # return translated map
  db-type: mysql                # reverse‑translation DB type
  mp-new: true                  # for MyBatis‑Plus 3.5.3.2+

Mark entity classes that need translation by implementing TransPojo and adding @Trans annotations. Example:

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

Example Scenarios

Simple Translation (ID → name)

@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, the translated fields appear in the transMap of the response.

Dictionary Translation (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 Translation

public enum SexEnum {
    MAN(1, "男"),
    WOMAN(0, "女");
    private Integer code;
    private String desc;
}

@Trans(type = TransType.ENUM, key = "desc")
private SexEnum sexEnum;

Cross‑service RPC Translation

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

Custom Data Source (AUTO_TRANS)

# application.yml
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) {
        return userMapper.selectBatchIds(ids);
    }
}

Performance Optimization

Enable caching on data‑source entities with @TransDefaultSett to avoid repeated database lookups.

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

When cache is active, only the main table is queried; related data is served from Redis, resulting in a noticeable reduction in response latency.

Summary

Zero‑intrusion annotation‑driven framework; minimal additional code.

All common translation scenarios (single‑table, dictionary, enum, RPC, custom) are covered.

Batch queries combined with Redis caching provide high performance without manual optimization.

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.

backendJavaSpringBootannotationData TranslationEasy-Trans
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.