英文:
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_id
和branch_id
的BankDetails
对象。
为了将其存储为一个对象,我已经修改了BankDetails
类,包括bank_id
和branch_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 & 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 & setters
}
I've also modified @PostMapping
method in @RestController
to feed the data.
@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) {
// 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论