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

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

Spring boot RequestBody with object fields that could be needed

问题

以下是您要求的翻译内容:

如何使我的 RequestBody 能够接受具有可能包含或不包含的对象字段的模型?

这是我正在调用的 RestController 端点:

  1. @PatchMapping(value = "/projects/update/{projectNumber}")
  2. public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody RequestReviewUpdate rru)
  3. {
  4. return reviewUpdateService.updateReview(projectNumber, rru);
  5. }

这是 @RequestBody 正在接受的模型:

  1. public class RequestReviewUpdate
  2. {
  3. private UpdateProject project;
  4. private List<UpdateHlsd> reviews;
  5. private List<UpdateProjectLeadership> stakeHolders;
  6. public UpdateProject getProject()
  7. {
  8. return project;
  9. }
  10. public List<UpdateHlsd> getReviews()
  11. {
  12. return reviews;
  13. }
  14. public List<UpdateProjectLeadership> getStakeHolders()
  15. {
  16. return stakeHolders;
  17. }
  18. }

我希望能够发送一个 JSON,其中可以包含所有对象字段,也可以是其中一些。

我的 JSON 主体可能如下所示:

  1. {
  2. "project": {
  3. "type": "HLSD"
  4. },
  5. "reviews": [
  6. {
  7. "id": 570,
  8. "requestedBy": "Name here",
  9. "notes": "test"
  10. }
  11. ],
  12. "stakeHolders": [
  13. {
  14. "id": 1088,
  15. "projectResource": "sid"
  16. }
  17. ]
  18. }

或者像这样:

  1. {
  2. "reviews": [
  3. {
  4. "id": 570,
  5. "requestedBy": "name",
  6. "notes": "test"
  7. }
  8. ]
  9. }

或任何其他可能的组合。

英文:

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

  1. @PatchMapping(value = &quot;/projects/update/{projectNumber}&quot;)
  2. public ResponseEntity&lt;Object&gt; updateProject(@PathVariable int projectNumber, @RequestBody RequestReviewUpdate rru)
  3. {
  4. return reviewUpdateService.updateReview(projectNumber, rru);
  5. }

Here is the model @RequestBody is accepting

  1. public class RequestReviewUpdate
  2. {
  3. private UpdateProject project;
  4. private List&lt;UpdateHlsd&gt; reviews;
  5. private List&lt;UpdateProjectLeadership&gt; stakeHolders;
  6. public UpdateProject getProject()
  7. {
  8. return project;
  9. }
  10. public List&lt;UpdateHlsd&gt; getReviews()
  11. {
  12. return reviews;
  13. }
  14. public List&lt;UpdateProjectLeadership&gt; getStakeHolders()
  15. {
  16. return stakeHolders;
  17. }
  18. }

I want to be able to send a JSON could contain all object field, or some.

My JSON body could look something like this...

  1. {
  2. &quot;project&quot;: {
  3. &quot;type&quot;: &quot;HLSD&quot;
  4. },
  5. &quot;reviews&quot;: [
  6. {
  7. &quot;id&quot;: 570,
  8. &quot;requestedBy&quot;: &quot;Name here&quot;,
  9. &quot;notes&quot;:&quot;test&quot;
  10. }
  11. ],
  12. &quot;stakeHolders&quot;: [
  13. {
  14. &quot;id&quot;: 1088,
  15. &quot;projectResource&quot;: &quot;sid&quot;
  16. }
  17. ]
  18. }

Or this...ect

  1. {
  2. &quot;reviews&quot;: [
  3. {
  4. &quot;id&quot;: 570,
  5. &quot;requestedBy&quot;: &quot;name&quot;,
  6. &quot;notes&quot;:&quot;test&quot;
  7. }
  8. }

Or any other possible combinations.

答案1

得分: 0

只需将您的 RequestReviewUpdate 设置为可选,如下所示:

  1. @PatchMapping(value = "/projects/update/{projectNumber}")
  2. public ResponseEntity<Object> updateProject(@PathVariable int projectNumber, @RequestBody(required=false) RequestReviewUpdate rru) {
  3. return reviewUpdateService.updateReview(projectNumber, rru);
  4. }

这将使您的参数变为可选,可以完全或部分地省略。也就是说,作为参数接收的对象的任何成员都可能存在或不存在。

英文:

Just make your RequestReviewUpdate optional, like:

  1. @PatchMapping(value = &quot;/projects/update/{projectNumber}&quot;)
  2. public ResponseEntity&lt;Object&gt; updateProject(@PathVariable int projectNumber, @RequestBody(required=false) RequestReviewUpdate rru) {
  3. return reviewUpdateService.updateReview(projectNumber, rru);
  4. }

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:

确定