Reputation: 692
When I read mapstruct documentation they say: MapStruct is a Java annotation processor for the generation of type-safe bean mapping classes.
https://mapstruct.org/documentation/stable/reference/html/#introduction
Which leaves me with the queston. Why do I need mapstruct? Jhipster uses it and I have no clue why did they need it for in the first place? Why you need a mapping inside Jhipster.
They also mention that .... Compared to writing mapping code from hand, MapStruct saves time by generating code which is tedious and error-prone to write. So it saves time but it does not explain why you need it, right?
Thanks. I hope they can modify the documentation with the doubts and explanations that are written down here.
Upvotes: 0
Views: 275
Reputation: 16294
JHipster uses MapStruct to generate code for mapping entities to/from DTOs as explained in https://www.jhipster.tech/using-dtos/
You can get rid of it by copying generated Mapper classes into your source tree and then evolving them manually. This could be useful if you don't plan to use JHipster beyond project bootstrapping and/or want to build DTOs that are too complex for MapStruct.
It might sound more work at first but it's simple code and you will need to do the same anyway in the frontend code.
Basically, a Mapper is a simple service that maps an entity to/from a Data Transfer Object. It does not require any library to do so, not to implement any specific interface, you just call writers from values you got from getters.
If you don't want to start from scratch, let's say you have defined a Book
entity, you can find an example by searching for the BookMapperImpl.java
class generated by MapStruct in your target
directory. Then, you can copy it to you src
directory, you get rid of mapstruct imports in BookMapperImpl
, you delete the BookMapper
interface and rename the BookMapperImpl
to BookMapper
.
Upvotes: 3