英文:
Spring boot RequestBody with object fields that could be needed
问题
以下是您要求的翻译内容:
如何使我的 RequestBody 能够接受具有可能包含或不包含的对象字段的模型?
这是我正在调用的 RestController 端点:
@PatchMapping(value = "/projects/update/{projectNumber}")
public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody RequestReviewUpdate rru)
{
return reviewUpdateService.updateReview(projectNumber, rru);
}
这是 @RequestBody 正在接受的模型:
public class RequestReviewUpdate
{
private UpdateProject project;
private List<UpdateHlsd> reviews;
private List<UpdateProjectLeadership> stakeHolders;
public UpdateProject getProject()
{
return project;
}
public List<UpdateHlsd> getReviews()
{
return reviews;
}
public List<UpdateProjectLeadership> getStakeHolders()
{
return stakeHolders;
}
}
我希望能够发送一个 JSON,其中可以包含所有对象字段,也可以是其中一些。
我的 JSON 主体可能如下所示:
{
"project": {
"type": "HLSD"
},
"reviews": [
{
"id": 570,
"requestedBy": "Name here",
"notes": "test"
}
],
"stakeHolders": [
{
"id": 1088,
"projectResource": "sid"
}
]
}
或者像这样:
{
"reviews": [
{
"id": 570,
"requestedBy": "name",
"notes": "test"
}
]
}
或任何其他可能的组合。
英文:
How can I make my RequestBody accept a model with object fields that may or may not be included?
This is the Restcontroller endpoint I’m calling
@PatchMapping(value = "/projects/update/{projectNumber}")
public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody RequestReviewUpdate rru)
{
return reviewUpdateService.updateReview(projectNumber, rru);
}
Here is the model @RequestBody is accepting
public class RequestReviewUpdate
{
private UpdateProject project;
private List<UpdateHlsd> reviews;
private List<UpdateProjectLeadership> stakeHolders;
public UpdateProject getProject()
{
return project;
}
public List<UpdateHlsd> getReviews()
{
return reviews;
}
public List<UpdateProjectLeadership> getStakeHolders()
{
return stakeHolders;
}
}
I want to be able to send a JSON could contain all object field, or some.
My JSON body could look something like this...
{
"project": {
"type": "HLSD"
},
"reviews": [
{
"id": 570,
"requestedBy": "Name here",
"notes":"test"
}
],
"stakeHolders": [
{
"id": 1088,
"projectResource": "sid"
}
]
}
Or this...ect
{
"reviews": [
{
"id": 570,
"requestedBy": "name",
"notes":"test"
}
}
Or any other possible combinations.
答案1
得分: 0
只需将您的 RequestReviewUpdate
设置为可选,如下所示:
@PatchMapping(value = "/projects/update/{projectNumber}")
public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody(required=false) RequestReviewUpdate rru) {
return reviewUpdateService.updateReview(projectNumber, rru);
}
这将使您的参数变为可选,可以完全或部分地省略。也就是说,作为参数接收的对象的任何成员都可能存在或不存在。
英文:
Just make your RequestReviewUpdate
optional, like:
@PatchMapping(value = "/projects/update/{projectNumber}")
public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody(required=false) RequestReviewUpdate rru) {
return reviewUpdateService.updateReview(projectNumber, rru);
}
This will make your parameter optional, either entirely or partially. I.e. any member of the object you receive as an argument, might be present or absent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论