Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mapstruct

I'm new to mapstruct. I'm trying to map ItemInfo to Item object and the following are my classes.

public class ItemInfo {

    private String itemOwner;
    private String itemOwnerArea;

    //getters and setters
}

Following is the class that I'm planing to convert to

public class Item {
    private UserInfo owner;
    // getters and setters.
}

UserInfo class (Note that it has two constructors)

public class UserInfo {

    private final String username;
    private final String area;

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

    public UserInfo(String info) {
        //set
    }

    // getters only
}

I have created the mapper interface as below

@Mapper
public interface ItemMapper {
    ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);

    @Mapping(source = "itemOwner", target = "owner.username")
    @Mapping(source = "itemOwnerArea", target = "owner.area")
    Item mapItemInfoToItem(ItemInfo itemInfo);
}

When I build this, I get the following error

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

Can you guide me please?

Update.

As mentioned by @AnishB I added a default constructor but I get a different error

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

Thanks

like image 566
Chamila Adhikarinayake Avatar asked Mar 02 '23 22:03

Chamila Adhikarinayake


2 Answers

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 
}
like image 178
Filip Avatar answered May 11 '23 02:05

Filip


Mapstruct needs a default empty constructor for its functioning.

So, update the UserInfo class to have a default empty constructor:

public class UserInfo {

    private final String username;
    private final String area;

    public UserInfo(){
        
    }

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

    public UserInfo(String info) {
        //set
    }

    // setters and getters
}

Or annotate anyone constructor with @Default so that mapstruct can use that constructor as a default constructor.

For example :

 @Default
 public UserInfo(String info) {
    //set
 }

Also, you have to add setters in UserInfo as well.

like image 22
Anish B. Avatar answered May 11 '23 03:05

Anish B.