问题
In my team lot of fights I see as some people using @JsonInclude
annotated objects in DAO
layer and some people argues to not to use and I could not able to find the reason for this.
Please anyone suggests.
回答1:
Are the @Json annotations on DTO objects that you use to define your DAO interfaces? It might indicate that you reuse the same objects to define an external API like REST.
A consideration might be that if you reuse these objects, that you will couple the REST API to the DAO API. You need to consider if this is acceptable for you.
For applications that you have completely under control, it might be acceptable.
If you have clients that need a stable version of the REST API, you might want to use different DTO objects for your REST API (with @Json annotations) and for your DAO, so you can change your DAO's interfaces without having to change your REST API. But you need to maintain double classes and have extra mapping code to map between them.
回答2:
Jackson
does not force developer to link domain model with it's annotations. In most cases you can use MixIn feature and add annotation to given class, methods, properties in Runtime.
There is no "right" approach you can use in every project. Generally, it depends from: team, business requirements, used frameworks, etc.
If you want to show in REST API
model as it is used for DB
you can use Jackson
annotations directly on given class as you probably do with Hibernate
annotations. But in case you just want to show some part of a model in REST API
you can create a separate model and use some mapping tools (Dozer, ModelMapper, MapStruct) or use MixIn
feature, I mentioned about before, and reuse ORM
model.
Definitely, you should use in a project one approach. Every class is annotated with Jackson
or none of them. Using it partially makes a mess and it is hard to maintain project like this.
To make decision easier, you can read:
- Domain Driven Design by Martin Fowler
- Domain Driven Design and Development In Practice
- Jackson/Hibernate, meta get methods and serialization
来源:https://stackoverflow.com/questions/59300896/what-is-wrong-if-dao-deals-with-jsoninclude-annotated-objects