mapstruct / mapstruct.org Goto Github PK
View Code? Open in Web Editor NEWWeb site of the MapStruct project
Web site of the MapStruct project
In the IDE Support we should add the answer from this SO question.
I just noticed that the links to the 1.3.0.Final guides still contain dev
in the URL and for 1.2.0.Final it is stable
:
1.3.0.Final: http://mapstruct.org/documentation/dev/reference/html/
1.2.0.Final: http://mapstruct.org/documentation/stable/reference/html/
I'm starting afresh with MapStruct version 1.0.0.Final
and receive the following error when I attempt to execute the codebase,
java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation
My Maven POM has the following added already,
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<configuration>
<defaultOutputDirectory>
${project.build.directory}/generated-sources/annotations
</defaultOutputDirectory>
<processors>
<processor>org.mapstruct.ap.MappingProcessor</processor>
</processors>
<options>
<mapstruct.suppressGeneratorTimestamp>true</mapstruct.suppressGeneratorTimestamp>
<!-- <mapstruct.defaultComponentModel>cdi</mapstruct.defaultComponentModel> -->
</options>
</configuration>
<executions>
<execution>
<id>process</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
</plugin>
Please note that I've modified the defaultOutputDirectory to be ${project.build.directory}/generated-sources/annotations (added an extra 'annotations`). If not adding that suffix, I am generating two sets of implementation, one at the generated-sources level and another at ..generated-sources/annotations level and that causes duplicate implementation exists error like below during compilation of the project,
error: duplicate class: a.g.x.h.t.mappers.DMapperImpl
The error is occurring when invoking the Mapper class using the below even though I could very well see the Implementations of the Mapper classes at ...target../generated-sources/annotations directory,
DMapper.INSTANCE.sourceToDestination(source);
mvn compile
on my project to identify the errors/warningsHi,
I have noticed interesting befaviour that I don't know if it is intended or not. I will try to explain it with example:
//first mapper
@Mappings({ @Mapping(source = "game.players", target = "players", qualifiedBy = QualifyPlayerMapper.class) })
TargetGame mapGame(Game game);
//second mapper
@Mappings({ @Mapping(source = "player.street", target = "addressStreet")})
TargetPlayer mapPlayer(Player player);
//some mapping method that I wanna use and it is qualiffied
@QualifyPlayerMapper
TargetPlayer mapPlayerMyWay(Player player);
so in the generated class, in the playerCollectionToPlayerCollection method, is used mapPlayer.
In my opinion it should use the second method - mapPlayerMyWay
Regards,
Natalia
I think that it would be nice to have a development glossary on our website in order to ease the contribution process. With this "glossary" we can shortly write what each of our terms mean.
This is just an idea that is open for discussion 😄
In Documentation Page , the contents section on the right is not vertically scrollable. On a 15'' wide screen, the screen height is not big enough to hold all the items; the contents section is cropped on point 15.
Source : {
someList: ArrayList<SomeObject>
}
Target: {
someList: ArrayLIst<SomeObject>
}
When I use the mapstruct, it will automatically create a new ArrayList and add the list from source in it. However, I just want to use the usual mapping (I want them to point to the same memory location), is there some attribute in mapper that I can configure so that mapstruct will generate
target.setSomeList(source.getSomeList())
?
Thank you very much!
As written in mapstruct/mapstruct#1145. Do we want to add an FAQ about the problem described there and reference the IntelliJ issue?
We still have some links pointing to cloudbees for our CI, which actually moved to Travis CI.
Check if there are other dead links.
See also this twitter thread
I have something like following structure:
class PersonDto {
final Value name = new Value();
final Value surname = new Value();
}
class Value {
...
void set(String value) {
...
}
String get() {
...
}
}
class Person() {
String name;
String surname;
}
Any clue how to map it? From PersonDto to Person, it is relatively easy, hot to do it in revered order. Mind the Values are final.
We had to map a lot of jaxb-types with the default-naming-strategy of jaxb (underscores to triple-underscores, ...) and didnt want to map all the Enum manually by the Mapping-annotation.
So we introduced EnumNamingStrategy, simmilar to AccessorNamingStrategy.
We also fixed a typo.
PageInfoDto
public class PageInfoDto<T> implements Serializable {
private int currentPageNum;
private int totalPageNum;
private int perPageNum;
private int totalItemNum;
private List<T> list;
}
Page
public class Page<T> implements Serializable {
private int current;
private int total;
private int size;
private int items;
private List<T> list;
}
say, i have a school list and a student list.
i want to map Page to PageInfoDto and map Page to PageInfoDto。
this is how my mapper looks like;
@Mapper( config = CentralConfig.class, uses = {StudentMapper.class, SchoolMapper.class}, componentModel = "spring")
public interface PageInfoMapper {
@Mappings({
@Mapping(target = "list", source = "pageInfo.records"),
@Mapping(target = "currentPageNum", source = "pageInfo.current"),
@Mapping(target = "totalPageNum", source = "pageInfo.pages"),
@Mapping(target = "perPageNum", source = "pageInfo.size"),
@Mapping(target = "totalItemNum", source = "pageInfo.total"),
})
PageInfoDto<SchoolDto> toPageInfoDto1(Page<School> pageInfo);
@Mappings({
@Mapping(target = "list", source = "pageInfo.records"),
@Mapping(target = "currentPageNum", source = "pageInfo.current"),
@Mapping(target = "totalPageNum", source = "pageInfo.pages"),
@Mapping(target = "perPageNum", source = "pageInfo.size"),
@Mapping(target = "totalItemNum", source = "pageInfo.total"),
})
PageInfoDto<StudentDto> toPageInfoDto2(Page<Student> pageInfo);
}
this obviously not a clever way to do . is there a better way to do this?
Instead of explicitly linking it will be good if we can have a link for the latest reference documentation for MapStruct. For example now we have http://mapstruct.org/documentation/1.1/reference/html/index.html, which can stay the same, but we will also have http://mapstruct.org/documentation/latest/reference/html/index.html that always points to the latest version of the documentation. This could help when linking the documentation to questions and/or blog posts. WDYT?
I am sorry that this error occurred during the use of mapstruct.
Environment used : gradle-4.6 STS 3.9.5 springBoot 2.0.4.RELEASE
plugins {
id 'java'
id 'net.ltgt.apt' version '0.9'
}
dependencies {
compile 'org.mapstruct:mapstruct-jdk8:1.3.0.Beta1'
compile 'org.mapstruct:mapstruct-processor:1.3.0.Beta1'
}
Referring to the official Web example, implementation classes can be generated, but this error always occurs when I use the injected bean.
I guess it's related to the environmental version I use. Friends who want to know can help answer? Thanks!!! @filiphr
Once we have the PR mapstruct/mapstruct-examples#25 added to the examples it would be nice to have an addition to the FAQ on the website with the required steps.
Moved from mapstruct/mapstruct#501.
Is this still relevant?
Hi,
I'm using jhipster, which generates mapstruct mappings.
I've errors on certain fields (not all of them), if they have multiple @mapping annotation. Only in the EDI. All building process, by maven, is OK.
The error is : "Incompatible types: int cannot be converted to boolean".
Have you any idea ?
Thanks,
Regards.
It can happen that someone is using the javax.inject.Named
instead of org.mapstruct.Named
. I think having this in our F&Q would be good.
I think it could be useful to specify that @qualifier import is org.mapstruct.Qualifier
In my project, I made the mistake of importing javax.inject.Qualifier which leads to the error message "Ambiguous mapping method..."
They seem to be not covered in the reference documentation.
See mapstruct/mapstruct#1207 for how it can be done.
Could I configure the mapper to map the target to null
if the source of a type String
is empty ""
?
When using the Lombok agent for Eclipse there are exceptions like java.lang.NoClassDefFoundError: org/mapstruct/ap/spi/AstModifyingAnnotationProcessor
We can add mapstruct/mapstruct#1159 (comment) to our FAQ as a workaround. Maybe even our Eclipse section
Resources I am aware of:
A lot of users are having problems, because they don't quite understand the maven compiler annotationProcessorPaths
. @alexis-puska suggested that we extend the installation instructions with something like:
<configuration>
<source>1.6</source> <!-- or higher, depending on your project -->
<target>1.6</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- If you use other annotations processor add them here -->
</annotationProcessorPaths>
</configuration>
I think that this is a good suggestion and would help other developers in issues when MapStruct and Lombok are not working well together due to wrong setup
Hi,
I'm working with Mapstruct 1.2.0.Final and I'm using a "defensive" programming style for a Map field in a class, for security reasons. I mean the field getter returns a copy of the field mappings, and the setter copies the given mappings into the field. Typically, this is done with such getter/setter in the class:
private Map<String, Object> properties;
public Map<String, Object> getProperties() {
return new HashMap<>(this.properties);
}
public void setProperties(final Map<String, Object> properties) {
this.properties.clear();
this.properties.putAll(properties);
}
Mapping another object to an existing instance of this class doesn't work as I expected, because the field setter in never used in implementations generated by Mapstruct. The code below is actually generated:
target.getProperties().clear();
target.getProperties().putAll(...);
To me, this shall be replaced by the policy below:
final Map<String, Object> properties = target.getProperties();
properties.putAll(...);
target.setProperties(properties);
I attached a test case in this issue:
MappingTest.java.zip
Is this feature implemented?
How can I configure/use Mapstruct for such case?
Thanks in advance for your help.
BR
The links for Hibernate and RedHat are somehow using the mapstruct website as domain.
E.g. using http://imulus.github.io/retinajs/
I have the following Java class structure and it's respective mapstruct mapping. Here source and destination beans in mapping are same. I only need to map specific fields to send to a client based on their requirement.
@XmlSeeAlso({A.class, B.class})
public class C extends D implements Serializable {
//50 odd properties
// getters&Setters
}
public class A extends C{
//properties
//getters&setters
}
public class B extends C{
//properties
//getters&Setters
}
public class D{
//properties
//getters&Setters
}
@Mapper
public interface CMapper{
C cToC(C c);
A aToA(A a);
B bToB(B b);
D dToD(D d);
//and remaining requrired mappers here
}
public class CMapperImpl implements CMapper{
public C cToC(C c){
}
//mapping logic
A aToA(A a){
//mapping logic
}
B bToB(B b){
//mapping logic
}
D dToD(D d){
//mapping logic
}
}
Code is being generated for all the defined methods in mapper interface. But methods which are defined for mapping A,B classes into C are not being used/called anywhere in the generated code. So fields of those two classes are not being mapped to resultant bean. This works fine when i use dozer. I had to simply specify bean mapping for both A&B with required fields in config xml file. However, It's not working with mapStruct.
@gunnarmorling @sradi @chschu
There are people that use MapStruct, Android and Proguard. I think that would be good if we add mapstruct/mapstruct#1323 (comment) to the FAQ
As written in mapstruct/mapstruct#926. MapStruct also does mapping between static properties. One can use custom AccessorNamingStrategy
that will make sure that the Executable
is not considered for mapping.
There were some issues when I used a newer version of hugo. Therefore, until now We were using 0.18. The problem was some new property (disablePathToLower
, which by default is false
) which caused the paths of the released articles to be lower case. We have fixed this.
In any case we need to add the version of hugo in our publish.sh
so we are always using a fix version when doing a publish.
It seems that there is a problem when both mapstruct
and mapstruct-jdk8
are found on the classpath. See longer discussion in mapstruct#841. A known problem is the compile dependency on mapstruct
in springfox-swagger. I think having this question and an example how to exclude the mapstruct
dependency is good to have.
I'm using mapstruct along with lomback with versions as follows
Java 1.8
springBootVersion = '1.4.3.RELEASE'
gradle version 3.4
mapstructVersion 1.3.0.Beta2
lombokVersion 1.18.0
Configurtions added
dependencies {
classpath('net.ltgt.gradle:gradle-apt-plugin:0.8')
}
plugins {
id 'net.ltgt.apt' version '0.9'
}
ext {
mapstructVersion = "1.3.0.Beta2"
lombokVersion = "1.18.0"
}
apply plugin: 'net.ltgt.apt'
compile "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
apt "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
sourceSets {
main.java.srcDirs += "build/generated/source/apt/main"
}
Still, I see the implementation class is not found. Can you please suggest
@larswetzer DYT you could create an update site which we can publish on the web site until we have the plug-in in the market place?
I like what Django is doing: https://docs.djangoproject.com/en/1.8/intro/overview/#install-it
A useful resource: http://webmasters.stackexchange.com/questions/99867/how-to-correctly-mark-up-different-versions-of-the-same-document-which-are-non-c
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.