Pass ID或具有不相关细节的对象,如Rest Call中的RequestBody?

问题描述 投票:1回答:1

方法1:

@PostMapping("/api/{id}")
String getSomeObj(int id){
    //make another rest call with id and get CustomObj
    // then do some logic and return something
    //Here response time will be more as it has again another rest calls

}

方法2:

@PostMapping("/api/{id}")
String getSomeObj(@PathParam("id") int id, @RequestBody CustomObj obj){
    //directly do logic with the provided obj and return something
    //Here Response time would be less as we are directly getting the actual Object from Request Body
    //BUT is this a good practise to pass an object in which we need only few details?
}

Q1)我要问的是只传递id还是Object?如果传递了id,则不必要进行另一个Rest调用。如果通过了Object,我们可以避免再进行一次rest调用,但是问题是:这个自定义对象也可能包含一些无关紧要的细节。.那么,这正确吗?

Q2)如果使用id传递,则与仅传递对象相比,响应时间会更长。.因此,我不知道应该采用哪种方法。

方法1:@PostMapping(“ / api / {id}”)字符串getSomeObj(int id){//用id进行另一个rest调用并获取CustomObj //然后执行一些逻辑并返回一些内容//此处响应时间将...

rest spring-boot oop spring-restcontroller spring-rest
1个回答
0
投票

A1)这完全取决于您,没有正确的方法。我要说的是,如果它是一个小物体,则传递该物体并快速响应。如果它是一个大对象,则通过id。您如何定义大小对象?如果对象中包含哈希图或列表,那是一个大对象。您也可以忽略内部序列化;检查https://www.baeldung.com/jackson-ignore-properties-on-serialization

© www.soinside.com 2019 - 2024. All rights reserved.