我对每个json负载都有DTO。我通过http发布请求获取有效负载。我得到两个有效载荷,但是如何将它们组合成最终有效载荷

时间:2019-05-02 00:11:17

标签: java json spring-boot lambda mapping

我有有效载荷1,有效载荷2和有效载荷3的DTO。     如何获得有效载荷3?     使用有效负载1和有效负载2。 如何建立数据模型之间的映射?

DTO 1

public class DataRequestModel {
private DataValue data; //some example
private long  timestamp;
@Data
public static class DataValue {
private Value1 value1;
private Value2 value2;
}
@Data
public static class Value1 {
    private long val;
}
@Data
public static class Value2 {
    private long val;
}
} 

数据模型2

public class Guid{
private string guid; 
} 

数据模型3

 public class payload{
 private String guid;
 private long longvalue;
 private long timestamp;
 }
## Controller
@RestController
@RequestMapping("/users")
public class DataTransferController {
 List<Payload> payload;
 @PostMapping("/list")
 public void data(@RequestBody DataRequestModel dataRequestModel){

不确定如何从这里开始...

       for(Payload temp: payload){
         temp.getLongValue()=dataRequestModel.getData().
         }
 }
 }  

这些是JSON的结构 1.JSON有效负载1

  {  
  "data":{  
 "value1":{  
    "val":30
 }, "value2":{  
 "val":20
 }
  },
 "timestamp":155501523
}

2.JSON负载2

       {  
       "guid":[  
       "/value1",
       "/value2"
       ]
       }

3.JSON最终有效负载(待发送)

     [  
     {  
     "guid":"/value1",
     "longvalue":30,
     "timestamp":155501523
     },
     {  
     "guid":"/value2",
     "longvalue":20,
     "timestamp":155501523
     }
     ]

我用于生成有效负载3的代码是

   @RestController @RequestMapping("/users")
 public class DataTransferController 
    { 
    List<Payload> payload; @PostMapping("/list")

    public void data(@RequestBody DataRequestModel dataRequestModel)
    { 
    Not sure how to do from here... for(TimeseriesPayload temp: timeseriesPayload)
{ 
temp.getLongValue()=dataRequestModel.getData().
 } 
}
 }

1 个答案:

答案 0 :(得分:1)

您无需循环即可执行此操作,请在public void data中创建List<Payload>

public void data(@RequestBody DataRequestModel dataRequestModel)  {

  List<Payload> list = new ArrayList<>();
  // first object
  Payload payload1 = new Payload();
  payload1.setGuid(//set value);
  payload1.setLongValue(dataRequestModel.getData().getValue1().getVal());
  payload1.setTimestamp(dataRequestModel.getTimestamp());
  list.add(payload1);
  // second object
  Payload payload2 = new Payload();
   payload12setGuid(//set value);
  payload2.setLongValue(dataRequestModel.getData().getValue2().getVal());
  payload1.setTimestamp(dataRequestModel.getTimestamp());
  list.add(payload2);

然后最后返回List