注意:在工具类里面注入bean实现
一、如果直接按照下面的方式注入bean,拿到的bean会是null
public class JsonTransformerUtils {
@Autowired
private SysColumnCfgService sysColumnCfgService;
.....
}
二、可以采用下面方式在工具类注入bean
1、工具类注入bean
public class JsonTransformerUtils {
private SysColumnCfgService sysColumnCfgService;
// 配置类构造函数
public JsonTransformerUtils(SysColumnCfgService sysColumnCfgService) {
this.sysColumnCfgService = sysColumnCfgService;
}
.....
}
2、工具类的使用
@RestController
public class JsonTransformerController {
private SysColumnCfgService sysColumnCfgService;
@Autowired
public JsonTransformerController(SysColumnCfgService sysColumnCfgService) {
this.sysColumnCfgService = sysColumnCfgService;
}
@PostMapping("/transform")
public String transformJson(@RequestBody String inputJson) {
JsonTransformerUtils jsonTransformerUtils = new JsonTransformerUtils(sysColumnCfgService);
String transformedJson = jsonTransformerUtils.transformJson(inputJson);
return transformedJson;
}
}