What is a good strategy for converting jpa entities into restful resources

前端 未结 4 1089
小鲜肉
小鲜肉 2020-12-13 01:03

Restful resources do not always have a one-to-one mapping with your jpa entities. As I see it there are a few problems that I am trying to figure out how to handle:

4条回答
  •  鱼传尺愫
    2020-12-13 01:16

    I agree with the other answers that DTOs are the way to go. They solve many problems:

    1. Separation of layers and clean code. One day you may need to expose the data model using a different format (eg. XML) or interface (eg. non web-service based). Keeping all configuration (such as @JsonIgnore, @JsonidentityInfo) for each interface/format in domain model would make is really messy. DTOs separate the concerns. They can contain all the configuration required by your external interface (web-service) without involving changes in domain model, which can stay web-service and format agnostic.

    2. Security - you easily control what is exposed to the client and what the client is allowed to modify.

    3. Performance - you easily control what is sent to the client.

    4. Issues such as (circular) entity references, lazily-loaded collections are also resolved explicitly and knowingly by you on converting to DTO.

提交回复
热议问题