我有有效载荷 1、有效载荷 2 和有效载荷 3 的 DTO。如何获得有效载荷3?使用payload 1和payload 2。如何建立数据模型之间的映射关系?DTO 1public class DataRequestModel {private DataValue data; //some exampleprivate long timestamp;@Datapublic static class DataValue {private Value1 value1;private Value2 value2;}@Datapublic static class Value1 { private long val;}@Datapublic static class Value2 { private long val;}} 数据模型 2public 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 payload1的结构 { "data":{ "value1":{ "val":30 }, "value2":{ "val":20 } }, "timestamp":155501523}2.JSON有效载荷2 { "guid":[ "/value1", "/value2" ] }3.JSON最终payload(待发送) [ { "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 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
您不需要 for 循环来执行此操作,在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
添加回答
举报
0/150
提交
取消