在@RequestBody对象中存储@PathVariable的值,是一种好的做法吗?

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

Storing values of @PathVariable in @RequestBody object, a good practice?

问题

我正在编写一个POST API,它位于我的@RestController中。

遵循REST最佳实践,URL是:

/v1/myapi/bank/{bank_id}/branch/{branch_id}

API还接受其他参数,以@RequestBody BankDetails的形式作为输入。

class BankDetails {
    String address;
    String zip;
    // 构造函数,getter和setter方法
}

出于某种原因,我想将此请求放入队列(在这种情况下是AWS SQS),即带有关联的bank_idbranch_idBankDetails对象。

为了将其存储为一个对象,我已经修改了BankDetails类,包括bank_idbranch_id

如下所示:

class BankDetails {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    String bankId;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    String branchId;

    String address;
    String zip;
    // 构造函数,getter和setter方法
}

我还修改了@RestController中的@PostMapping方法以提供数据。

@PostMapping(value = "/v1/myapi/bank/{bank_id}/branch/{branch_id}", produces = "application/json")
public ResponseEntity<MyResponse> createBankBranch(
    @PathVariable(name = "bank_id") String bankId,
    @PathVariable(name = "branch_id") String branchId,
    @RequestBody BankDetails bankDetails) {

    // 添加了这两行。
    bankDetails.setBankId(bankId);
    bankDetails.setBranchId(branchId);
}

首先在路径中获取参数,然后将其传递给RequestBody对象,这是否是一个良好的实践?

附言:我不能使用/v1/myapi/bank/{bank_id},因为那是一个单独的API。

英文:

I'm writing a POST api in my @RestController.

Following rest best practice,the url is

/v1/myapi/bank/{bank_id}/branch/{branch_id}

The API also takes other parameters as input in the form of @RequestBody BankDetails.

class BankDetails {
    String address;
    String zip;
    // Constructor, getters &amp; setters
}

For some reason I want to put this request into a queue (AWS SQS in this case), i.e. BankDetails object with associated bank_id & branch_id.

For storing it as one object, I've modified the BankDetails class to include bank_id & branch_id.

Like:

class BankDetails {
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    String bankId;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    String branchId;

    String address;
    String zip;
    // Contructor, getters &amp; setters
}

I've also modified @PostMapping method in @RestController to feed the data.

@PostMapping(value = &quot;/v1/myapi/bank/{bank_id}/branch/{branch_id}&quot;, produces = &quot;application/json&quot;)
public ResponseEntity&lt;MyResponse&gt; createBankBranch(
    @PathVariable(name = &quot;bank_id&quot;) String bankId,
    @PathVariable(name = &quot;branch_id&quot;) String branchId,
    @RequestBody BankDetails bankDetails) {

        // Added these 2 lines.
        bankDetails.setBankId(bankId);
        bankDetails.setbrnachId(branchId);
}

Is this a good practice to first get parameters in path & feed the same in RequestBody object?

P.S. I cannot use /v1/myapi/bank/{bank_id}, because that's a separate API.

答案1

得分: 1

只需以DTO形式将所有细节作为POST /myapi/bank,无需担心任何事情。

我认为你没有遵循“最佳实践”,据我所知,你提供的路径应该用于例如在给定的银行ID上修改给定ID的分行,而不是银行本身。

例如,拥有PUT /bank/bankId/vault/cash可以修改给定银行中现金的总额(愚蠢,但我认为这是一个很好的示范),而不是银行本身,但这正是你试图做的。

英文:

Simply POST /myapi/bank and all the details as DTO and dont worry about anything.

I dont think you have followed "best practices" as AFAIK this path you have provided would be used to eg modify branch with given id in context of given bank_id, not the bank itself

For example, having PUT /bank/bankId/vault/cash could eg modify overall amount of cash in given bank (stupid, but i think its fine as showcase), not the bank itself, but this is what you try to do.

huangapple
  • 本文由 发表于 2023年2月6日 04:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355439.html
匿名

发表评论

匿名网友

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

确定