我有這樣的有效載荷
{
"eventId":"ep9_0579af51",
"eventTime":"5/11/2022 5:50:58 PM",
"eventType":"UpdateTransaction",
"meta":{
"userId":"vkp",
"resourceType":"Transaction/DataDocs"
}
}
我需要將此 json 欄位映射到單個物體類中。
@PostMapping(path = "/id", consumes = "application/json")
public ResponseEntity<ImportTrans> import(@RequestBody ImportTrans importTrans) {
return ResponseEntity.of(Optional.ofNullable(repository.save(importTrans););
}
@Table(name = "IMPORT_TRANS")
@Entity
public class ImportTrans implements Serializable {
@Id
private Long processId;// AutoGenerator
private String eventId;
private Date eventTime;
private String eventType;
private String userId; // I dont want create new class for meta . Is there any way i
//can access meta.userId in ImportTrans class.
private String resourceType;
}
如何在不為其創建單獨的類的情況下meta從中訪問資料?ImportTrans
uj5u.com熱心網友回復:
您應該在到達控制器之前修改您的請求正文。
“您必須在實施之前自行考慮應用程式性能因素”
選項 1. 使用 RequestBodyAdvice。
選項 2. 使用 Spring HandlerInterceptor。
選項 3. 使用 AOP
選項 4. 使用 HTTP 過濾器。
以下解決方案僅在您使用單獨的 DTO 類時才有效。
private Map<String, String> meta = new HashMap<>();
String userID = importTrans.getMeta().get("userId");
我希望以上資訊回答了你的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478772.html
