Spring Boot中的RequestBody与可能需要的对象字段

huangapple go评论90阅读模式
英文:

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 = &quot;/projects/update/{projectNumber}&quot;)
public ResponseEntity&lt;Object&gt; 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&lt;UpdateHlsd&gt; reviews;
	private List&lt;UpdateProjectLeadership&gt; stakeHolders;
	
	public UpdateProject getProject() 
	{
		return project;
	}
	
	public List&lt;UpdateHlsd&gt; getReviews() 
	{
		return reviews;
	}
	
	public List&lt;UpdateProjectLeadership&gt; 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...

{
    &quot;project&quot;: {
        &quot;type&quot;: &quot;HLSD&quot;
    },
    &quot;reviews&quot;: [
        {
            &quot;id&quot;: 570,
            &quot;requestedBy&quot;: &quot;Name here&quot;,
            &quot;notes&quot;:&quot;test&quot;
        }
        ],
    &quot;stakeHolders&quot;: [
        {
            &quot;id&quot;: 1088,
            &quot;projectResource&quot;: &quot;sid&quot;
        }
    ]
}

Or this...ect

{
    &quot;reviews&quot;: [
        {
            &quot;id&quot;: 570,
            &quot;requestedBy&quot;: &quot;name&quot;,
            &quot;notes&quot;:&quot;test&quot;
        }
 }

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 = &quot;/projects/update/{projectNumber}&quot;)
public ResponseEntity&lt;Object&gt; 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.

huangapple
  • 本文由 发表于 2020年8月28日 07:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63625451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定