简体   繁体   中英

how to process dictionary convert in the rest api in springmvc

in the system, it have some dictioanry data store in mysql, and i need to convert the field to its id so i can save the relation or report this convert is fail.To simplify the problem.i assume i the three dictioanry, that its ddl is

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

and i need to implement three api that the param include two combinations of them. they may be look like:

//dict1 is t_dict1's code
//dict2 is t_dict2's code
//dict3 is t_dict3's name

@RestController
public class ApiController {
    @PostMapping("api1")
    public void api1(Param1 param1){}

    @PostMapping("api2")
    public void api1(Param2 param2){}

    @PostMapping("api2")
    public void api1(Param3 param3){}
}

class Param1{
    private String dict1;
    private String dict2;
}
class Param2{
    private String dict2;
    private String dict3;
}
class Param3{
    private String dict1;
    private String dict3;
}

there are codes that map them to id,which used in multiple places.

  • code1 that query t_dict1's code
  • code2 that query t_dict2's code
  • code3 that query t_dict3's name

if there some mechanism or some design to eliminate such boilerplate code.

why not supply a parameter distinguishing different case.

class Param{
   Integer 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM