The same / different DTO object in creating, updating, and getting rest endpoints?

Consider the following UserDTO and UserController classes that provide endpoints for creating, updating, and retrieving a user.

Having the id property in the UserDTO class does not make sense for creating and updating. If I use swagger or other automatic generated API documentation, then this indicates that the identifier can be passed at the endpoint of creation. But the system does not use it, since identifiers are generated internally.

If I look at get, then maybe I can get rid of the id property, but this is definitely necessary at the endpoint of the list user.

I was thinking about returning the internal user domain object at the get / list endpoints. That way I can get rid of the id class of the UserDTO form.

Is there a better option I can use for this?

public class UserDTO {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@RestController
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Void> create(@RequestBody UserDTO user) {
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<UserDTO> get(@PathVariable("id") int id) {
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<Void> update(@PathVariable("id") int id, @RequestBody UserDTO user) {
    }
}

This question may have been asked, but I could not find. So excuse me for repeating the question.

+4
source share
2 answers

D ata T ransfer O bject (DTO) - , : , -. API REST DTO, .

, REST , persistence.

, , MapStruct, DTO API REST API / .

DTO API REST, :

DTO , :

+3

:

interface UserDTO {

    public String getName ();

    public void setName (String name);

}

interface IdentifiableUserDTO extends UserDTO {

    public Long getId ();

    public void setId (Long id);

}


class DefaultUserDTO implements IdentifiableUserDTO {

}

DTO:

@RestController
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Void> create(@RequestBody IdentifiableUserDTO user) {
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<UserDTO> get(@PathVariable("id") int id) {
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<Void> update(@PathVariable("id") int id, @RequestBody UserDTO user) {
    }
}
+1

Source: https://habr.com/ru/post/1625814/


All Articles