“Ambiguous constructors found” error when there more than one constructor when using MapStruct

We are working on improving the error message in this place.

From the current error message

Ambiguous constructors found for creating com.app.test.UserInfo. Either declare parameterless constructor or annotate the default constructor with an annotation named @Default.

You have 2 options:

  • Create an empty constructor and MapStruct will use it. This is however not needed and I would strongly recommend not to use it.
  • Create your own @Default annotation and annotate the default constructor that MapStruct should use. I would highly recommend this approach.

MapStruct is not shipping this annotation because it is focused on bean mapping and doesn’t want to pollute the entity code with annotations from MapStruct. Therefore it can use any Default annotation from any package.

e.g.

package foo.support.mapstruct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.CLASS)
public @interface Default {
}

You already did that and the next error message:

Property “username” has no write accessor in UserInfo for target name “owner.username”.

Is there saying that there is no write accessor for the username, because most likely you have annotated the single parameter constructor. If you’ve done that then MapStruct only knows about the area and not about the username. Another option for that error is that you have added an empty parameterless constructor and no setters.

You most likely need

@Default
public UserInfo(String username, String area){
    //set 
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top