Spring Rest PostMapping从json映射到多个表

时间:2019-01-15 07:51:15

标签: java rest spring-boot hashmap spring-rest

我想将地图格式的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);通常会保存,但是在这种情况下,同一行会出现以下错误:

  

方法保存在类型crudrepository中   不适用于参数(对象)

1 个答案:

答案 0 :(得分:0)

我改变了方法,问题是从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;

}