gpt4 book ai didi

java - Spring Rest PostMapping 从 json 映射到多个表

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:44 26 4
gpt4 key购买 nike

我想将 map 格式的json保存到数据库中。

我能够将不同不相关表中的数据@Getmapping转换为json对象,但我无法用一种方法将相同的json格式保存到两个表中。这是我的代码和 json:

@RestController
public class NewServiceController {

@Autowired
EsbcoreServiceRepository serviceRepo;

@Autowired
EsbcoreRuleRepository ruleRepo;

@Autowired
EsbcoreServiceDestinationRepository serviceDestinationRepo;

@GetMapping(value="/api/allservices")
public Map<String, Object> getAllServices() {
Map<String, Object> servicesMap=new HashMap<String, Object>();
servicesMap.put("services", serviceRepo.findAll());
servicesMap.put("rules", ruleRepo.findAll());
return servicesMap;
}

@PostMapping(value="/api/allservices")
public Object addnewservice(@RequestBody Map<String, Object> datamap) {
//Map<String, Object> servicesMap=new HashMap<String, Object>();
Object esbcoreRule=datamap.get("rules");
Object esbcoreService=datamap.get("services");
ruleRepo.save(esbcoreRule);
//serviceRepo.save(esbcoreService);
return datamap;
}
}

使用的 JSON:

{
"rules":
[
{
"ruleId": 906,
"ruleOrder": 9,
"serviceId":
{
"serviceId": 908
}

} ],
"services":
[

{
"serviceId": 908,
"serviceCode": "0221",
"systemDomainId": 37,
"serviceType": "SOAP",
"serviceName": "Customer",
"operation": "MandateReActivate",
"version": "1.0",
"sync": 1,
"logLevel": "DEBUG",
"duplicateCheck": 1,
"genericReply": 0,
"timeout": 30,
"retryMax": 0,
"retryInterval": 0,
"prjReleaseCode": "FINTECH_3.0.0",
"emailAlertGroup": null,
"smsAlertGroup": null,
"emailAlertFlag": 1,
"smsAlertFlag": 1,
"creationDate": "2018-11-08T04:37:20.000+0000",
"createdBy": "GGMAINA"
}
]
}

当返回类型为模型名称/数据库对象名称时,ruleRepo.save(esbcoreRule); 行通常会保存,但在这种情况下,同一行出现以下错误:

The method save(s) in the type crudrepository is not applicable for the arguments (object)

最佳答案

我改变了我的方法,问题在于从类型 Object 转换为 EsbcoreRule、EsbCoreService 等。我使用 gson 代替,这是我的代码:

 @PostMapping(value="/api/allservices")
public Object addnewservice(@RequestBody Map<String, Object> datamap)
{
//Gson gson= new Gson();
Gson gson = new GsonBuilder().create();

Object esbcoreService=datamap.get("services");
//Object esbcoreServiceT=datamap.get("servicesT");
Object esbcoreRule=datamap.get("rules");
Object esbcoreRuleCondition =datamap.get("ruleConditions");
Object esbcoreRuleDestination=datamap.get("ruleDestinations");
Object esbcoreServiceDestination=datamap.get("serviceDestinations");



EsbcoreRule rule= gson.fromJson(esbcoreRule.toString(),EsbcoreRule.class);
EsbcoreService service=gson.fromJson(esbcoreService.toString(),EsbcoreService.class);
//EsbcoreService serviceT=gson.fromJson(esbcoreServiceT.toString(),EsbcoreService.class);
EsbcoreRuleCondition ruleCondition=gson.fromJson(esbcoreRuleCondition.toString(),EsbcoreRuleCondition.class);
EsbcoreRuleDestination ruleDestination=gson.fromJson(esbcoreRuleDestination.toString(),EsbcoreRuleDestination.class);
EsbcoreServiceDestination serviceDestination=gson.fromJson(esbcoreServiceDestination.toString(),EsbcoreServiceDestination.class);


serviceRepo.save(service);
//serviceRepo.save(serviceT);
ruleRepo.save(rule);
ruleConditionRepo.save(ruleCondition);
ruleDestinationRepo.save(ruleDestination);
serviceDestinationRepo.save(serviceDestination);





return datamap;

}

关于java - Spring Rest PostMapping 从 json 映射到多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54194634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com