Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Spring MVC REST API 字典字段自动转换ID的实践与优化_创想鸟

Spring MVC REST API 字典字段自动转换ID的实践与优化

Spring MVC REST API 字典字段自动转换ID的实践与优化

本文探讨在spring mvc rest api中,如何高效且优雅地处理入参中字典字段(如`code`或`name`)到其对应id的转换,以消除重复的查询逻辑。我们将介绍一种基于自定义参数解析器(custom argument resolver)的解决方案,实现请求参数的自动化转换,从而简化控制器代码并提升系统可维护性。

引言:字典字段转换的挑战

在构建基于Spring MVC的RESTful API时,我们经常会遇到这样的场景:前端提交的数据中包含的是人类可读的字典项字符串(例如,”male”、”active”或某个区域的名称),但后端业务逻辑或数据库存储需要这些字典项对应的唯一标识符(ID)。例如,一个用户注册接口可能接收性别字段为”male”,但数据库中存储的是性别字典表中male对应的id。

原始问题描述了一个典型的多字典、多组合的转换需求:系统中有t_dict1、t_dict2、t_dict3三张字典表,API接口接收这些字典项的code或name字段,需要将其转换为对应的id。如果每次都在控制器方法内部手动执行查询和转换,会导致以下问题:

代码冗余: 相同的字典查询逻辑会在多个API方法中重复出现。职责不清: 控制器方法承担了参数转换的额外职责,偏离了其主要关注点(请求路由和业务逻辑协调)。维护困难: 字典结构或查询逻辑变更时,需要修改多处代码。可测试性差: 转换逻辑与控制器逻辑耦合,增加了单元测试的复杂性。

因此,我们需要一种机制来自动化和封装这些字典字段到ID的转换过程,从而简化控制器代码,提高系统的可维护性和可扩展性。

基础准备:字典数据与API定义

首先,我们根据问题描述,定义相关的字典表结构和API接口及其数据传输对象(DTO)。

字典表结构示例:

CREATE TABLE `t_dict1` (  `id` bigint NOT NULL AUTO_INCREMENT,  `code` varchar(50) NOT NULL UNIQUE,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `t_dict2` (  `id` bigint NOT NULL AUTO_INCREMENT,  `code` varchar(50) NOT NULL UNIQUE,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `t_dict3` (  `id` bigint NOT NULL AUTO_INCREMENT,  `name` varchar(50) NOT NULL UNIQUE,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

原始API接口及DTO定义:

// dict1 is t_dict1's code// dict2 is t_dict2's code// dict3 is t_dict3's name// 原始的DTO,字段为String类型class Param1 {    private String dict1;    private String dict2;    // Getters and Setters    public String getDict1() { return dict1; }    public void setDict1(String dict1) { this.dict1 = dict1; }    public String getDict2() { return dict2; }    public void setDict2(String dict2) { this.dict2 = dict2; }}class Param2 {    private String dict2;    private String dict3;    // Getters and Setters    public String getDict2() { return dict2; }    public void setDict2(String dict2) { this.dict2 = dict2; }    public String getDict3() { return dict3; }    public void setDict3(String dict3) { this.dict3 = dict3; }}class Param3 {    private String dict1;    private String dict3;    // Getters and Setters    public String getDict1() { return dict1; }    public void setDict1(String dict1) { this.dict1 = dict1; }    public String getDict3() { return dict3; }    public void setDict3(String dict3) { this.dict3 = dict3; }}@RestControllerpublic class ApiController {    // 假设这些方法内部会进行字典转换    @PostMapping("api1")    public String api1(@RequestBody Param1 param1) {        // 伪代码:String -> ID 转换逻辑会在这里重复出现        // Long dict1Id = dictionaryLookupService.getIdByCode1(param1.getDict1());        // Long dict2Id = dictionaryLookupService.getIdByCode2(param1.getDict2());        // ...        return "api1 processed";    }    @PostMapping("api2")    public String api2(@RequestBody Param2 param2) {        // 伪代码:String -> ID 转换逻辑会在这里重复出现        // Long dict2Id = dictionaryLookupService.getIdByCode2(param2.getDict2());        // Long dict3Id = dictionaryLookupService.getIdByName3(param2.getDict3());        // ...        return "api2 processed";    }    @PostMapping("api3") // 注意:原始问题有重复的api2,这里修正为api3    public String api3(@RequestBody Param3 param3) {        // 伪代码:String -> ID 转换逻辑会在这里重复出现        // Long dict1Id = dictionaryLookupService.getIdByCode1(param3.getDict1());        // Long dict3Id = dictionaryLookupService.getIdByName3(param3.getDict3());        // ...        return "api3 processed";    }}

可以看到,在每个API方法中,都需要手动将ParamX中的String字段转换为Long类型的ID。这正是我们需要消除的boilerplate代码。

Waymark Waymark

Waymark是一个视频制作工具,帮助企业快速轻松地制作高影响力的广告。

Waymark 79 查看详情 Waymark

核心组件:字典查找服务

为了封装字典查询逻辑,我们首先创建一个统一的字典查找服务。这个服务负责根据字典类型和字符串值(code或name)返回对应的ID。

字典类型枚举:

public enum DictType {    DICT1, // 对应t_dict1,通过code查询    DICT2, // 对应t_dict2,通过code查询    DICT3  // 对应t_dict3,通过name查询}

字典查找服务接口与实现:

import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.Map;import java.util.Optional;// 模拟的Repository接口interface DictRepository {    Optional findIdByCode1(String code);    Optional findIdByCode2(String code);    Optional findIdByName3(String name);}// 模拟的Repository实现@Serviceclass MockDictRepository implements DictRepository {    // 模拟数据    private final Map dict1Map = new HashMap();    private final Map dict2Map = new HashMap();    private final Map dict3Map = new HashMap();    public MockDictRepository() {        dict1Map.put("codeA", 101L);        dict1Map.put("codeB", 102L);        dict2Map.put("codeX", 201L);        dict2Map.put("codeY", 202L);        dict3Map.put("nameM", 301L);        dict3Map.put("nameN", 302L);    }    @Override    public Optional findIdByCode1(String code) {        return Optional.ofNullable(dict1Map.get(code));    }    @Override    public Optional findIdByCode2(String code) {        return Optional.ofNullable(dict2Map.get(code));    }    @Override    public Optional findIdByName3(String name) {        return Optional.ofNullable(dict3Map.get(name));    }}@Servicepublic class DictionaryLookupService {    private final DictRepository dictRepository;    public DictionaryLookupService(DictRepository dictRepository) {        this.dictRepository = dictRepository;    }    /**     * 根据字典类型和字符串值查找对应的ID     * @param dictType 字典类型     * @param value 字典项的code或name     * @return 对应的ID     * @throws IllegalArgumentException 如果找不到对应的ID     */    public Long lookupId(DictType dictType, String value) {        if (value == null || value.trim().isEmpty()) {            return null; // 或者抛出异常,取决于业务需求        }        Optional idOptional;        switch (dictType) {            case DICT1:                idOptional = dictRepository.findIdByCode1(value);                break;            case DICT2:                idOptional = dictRepository.findIdByCode2(value);                break;            case DICT3:                idOptional = dictRepository.findIdByName3(value);                break;            default:                throw new IllegalArgumentException("Unsupported dictionary type: " + dictType);        }        return idOptional.orElseThrow(() ->                new IllegalArgumentException("Dictionary item not found for type " + dictType + " with value '" + value + "'"));    }}

这个DictionaryLookupService将所有字典查询细节封装起来,并提供了统一的lookupId方法。在实际项目中,DictRepository会与JPA或MyBatis等持久化框架集成。

解决方案一:基于DTO和业务服务的传统方式

这是最直接也最常用的方式,通过在业务服务层进行DTO到领域模型的转换,并在此过程中调用DictionaryLookupService。

示例代码:

// 内部领域模型,包含ID字段class InternalParam1 {    private Long dict1Id;    private Long dict2Id;    // Getters and Setters    public Long getDict1Id() { return dict1Id; }    public void setDict1Id(Long dict1Id) { this.dict1Id = dict1Id; }    public Long getDict2Id() { return dict2Id; }    public void setDict2Id(Long dict2Id) { this.dict2Id = dict2Id; }}@Servicepublic class BusinessService {    private final DictionaryLookupService dictionaryLookupService;    public BusinessService(DictionaryLookupService dictionaryLookupService) {        this.dictionaryLookupService = dictionaryLookupService;    }    public void processApi1(Param1 param1) {        // 1. DTO到内部模型的转换和字典查找        InternalParam1 internalParam = new InternalParam1();        internalParam.setDict1Id(dictionaryLookupService.lookupId(DictType.DICT1, param1.getDict1()));        internalParam.setDict2Id(dictionaryLookupService.lookupId(DictType.DICT2, param1.getDict2()));        // 2. 执行核心业务逻辑,使用internalParam        System.out.println("Processing InternalParam1 with dict1Id: " + internalParam.getDict1Id() + ", dict2Id: " + internalParam.getDict2Id());        // ...    }    // 类似的 processApi2, processApi3 方法}@RestControllerpublic class ApiController {    private final BusinessService businessService;    public ApiController(BusinessService businessService) {        this.businessService = businessService;    }    @PostMapping("api1_traditional")    public String api1Traditional(@RequestBody Param1 param1) {        businessService.processApi1(param1);        return "api1 traditional processed";    }}

优点: 职责分离清晰,业务逻辑层专注于处理ID,控制器层专注于接收请求。缺点: 控制器仍然需要将原始DTO传递给业务服务,业务服务内部需要显式

以上就是Spring MVC REST API 字典字段自动转换ID的实践与优化的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1062710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月2日 05:39:44
史诗级更新上热搜:iPhone录音会通知对方引热议 苹果回应了
下一篇 2025年12月2日 05:39:45

相关推荐

发表回复

登录后才能评论
关注微信