vendredi 24 avril 2020

Spring Rest Does DTOs can be BiDirectional?

I am new to Spring REST Api. Couple of doubts:

DOUBT1:

class UserDTO {
    long id;
    String name;
    int age;

    // getters and setters
}

CASE1:

public void doSomethng(@RequestBody UserDTO userDTO){
    repository.findById(userDTO.getId());
}

CASE2:

public void doSomethng(@RequestBody long id){ //Is this correct?
    repository.findById(id);
}

CASE3:

public void doSomethng(@RequestBody Map<Long, Object> map){ //Is this correct?
    repository.findById(map.get("id"));
}

I actually need to pass only "id" for POST Request.

So, Out of above 3 cases, which is a good practice? for me long id seems reasonable. I am not understanding, in which cases Passing Map as RequestBody is much better than DTO?

DOUBT2: I am currently using DTOs as BiDirectional. So, Using Bidirectional for DTOs is correct? Will am I going to face any issues with below DTOs design?

class UserDTO {
    long id;
    List<OrderDTO> orders;
}

class OrderDTO {
    long orderId;
    UserDTO userDTO;
}

Aucun commentaire:

Enregistrer un commentaire