Questions tagged [mapstruct]
MapStruct is a code generator for creating efficient, type-safe bean-to-bean mappings in Java.
1,159
questions
1
vote
2
answers
30
views
Mapstruct conversion from Date to LocalDatetime
I'm trying to convert an object using a mapper with Mapstruct that has a Date (java.util.Date) field to an object with a LocalDateTime field. The problem is that it maps the time wrong because in the ...
0
votes
0
answers
10
views
MapStruct: using a Mapper with two sources as part of another mapper
I have an Entity (A) that has a collection of another entity (B), and equivalent Dtos for them.
I have a MapStruct mapper for ADto -> AEntity (which takes ADto) and BDto -> BEntity (which takes ...
-1
votes
1
answer
20
views
How to use the mapping in practice? [closed]
The example here: https://github.com/mapstruct/mapstruct-examples/blob/3457f37c24ea7799b0752cc7ee8307283ab09ad2/mapstruct-mapping-with-cycles/src/main/java/org/mapstruct/example/mapper/EmployeeMapper....
0
votes
0
answers
7
views
Map struct mapper not adding Many to One column for mapping
For my spring-boot application I want to convert DTO columns to entity object for that I am using mapstruct mapper class. but it is not converting my ManyToOne column to entity object. how can I add ...
0
votes
1
answer
32
views
MapStruct generates invalid mapper
I got the following two classes (I'm trying to map from 1st to 2nd):
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RegisterRequestDto {
private String username;
private String ...
1
vote
1
answer
33
views
mapper overwriting resolved object
i have a mapper which uses resolve factory to turn a dto (with its id populated) to a domain entity. It all seems to work but in the generated mapper impl, it overwrites the resolved properties from ...
0
votes
1
answer
18
views
mapstruct reporting unknown property but as far as i can tell its there
I have a fairly simple mapper
/**
* Interface TicketLocationDetailsMapper that allows mapping between DTO and Domain and vice versa.
*
* @author Owen Gerig / Dev Team <br>
* created ...
0
votes
1
answer
23
views
How to use @ConfigProperty in a MapStruct mapper?
I need a @ConfigProperty in my Mapper.
I cannot inject it, since the Mapper is an interface.
How can I solve this?
@ConfigProperty(name = "limit") // <- Does not work here
int limit;
@...
0
votes
1
answer
39
views
Prevent mapstruct from using the single object mapping function in list of object mapping function
I have 2 micro-services: let's call them A and B.
The entities handled by B in which there is a reference to an entity of A are implemented as a simple Long id (eg. the groupId)
@Getter
@Setter
@...
0
votes
1
answer
25
views
mapping from flat protobuf message to class with inner classes using mapStruct
I need to use mapStruct to write a mapping from message AddressProto to the class Address. But message AddressProto consists only of string fields while Address class has inner classes.
I have written ...
0
votes
1
answer
55
views
mapper does not convert between dto to entity
I am new to mapsturct I just want to map between dto to entity those two;
my bank class;
@Id
private int id;
my bank dto class;
private Integer bankId;
my mapper below;
BankMapper ...
1
vote
2
answers
73
views
Mapstruct - How to convert a DTO String parameter to an Entity object?
I'm new to Mapstruct and I'm trying to understand it properly.
What I want to achieve is converting from a DTO String parameter (carModel) to his Entity, retrieve using Service and Repository.
The ...
0
votes
1
answer
26
views
Accessing application property in Spring boot application from MapStruct mapper
I want to access basic-info.expiration from application.yml a Spring boot application.
application.yml
basic-info:
expiration: 6
UserMapper.java
@Mapper(componentModel = "spring")
public ...
0
votes
0
answers
15
views
MapStruct: Ambiguous factory methods error, XJC generated ObjectFactory
I would like to ask how to solve the problem with Ambiguous factory methods error, when the ObjectFactory is generated from xsd.
In my case I am even surprised that I am getting this error, because ...
3
votes
0
answers
74
views
Mapstruct with JAXB, @XmlElementRefs
I spent really a lot of time on this, but I can not find the answer. From documentation it seams it should work automatically, but for me it does not. I am facing following problem:
XSD:
<xsd:...
0
votes
0
answers
23
views
MapStruct with Groovy
I have a project with groovy classes and I want to use MapStruct for bean mapping. I see in other questions a working solution like this:
MapStruct with groovy classes
But for me, when I add:
...
1
vote
1
answer
24
views
MapStruct: initialize using updateMethod(), create even though source is null
could someone told me how (or if it is even possible) to force update method to create target if it is null?
I would like to achieve something like this:
@BeforeMapping
@Mapping(source = "...
0
votes
1
answer
28
views
MapStruct - Map Generic List to Non-Generic List containing different types
I am attempting to use MapStruct to convert between 2 internal models. These models are generated from the same specification and represent a large tree-like structure that is constantly being added ...
0
votes
0
answers
50
views
Mapping from map to bean using snake case to camel case strategy
I need to convert an object of Map<String,String> with keys like "some_att_name" to class object fields like someAttName.
I couldn't find an easy way to do this.
MapStruct does support ...
0
votes
1
answer
53
views
MapStruct in Kotlin: How to map from multiple sources into one model?
Let's say I have model:
data class MyModel(
var firstName: String,
var lastName: String,
)
And let's say I'm attempting to map some properties into this model from my source PersonInfo
public ...
0
votes
1
answer
39
views
MapStruct - mapping nested List<String>
How can I use MapStruct to map nested List?
Here is my entity class:
public class ChannelEntity {
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable
List<String> ...
0
votes
1
answer
49
views
Erroneous element error with mapstruct and OpenAPI
While trying to convert the java model generated by openapi-generator with MapStruct getting the following exception
error: No implementation was created for EmployeeMapper due to having
a problem in ...
2
votes
2
answers
99
views
MapStruct custom list mapping for bi-directional Hibernate association
What is the best way to perform MapStruct mapping from a Data Transfer Object (DTO) to a Hibernate entity with a bi-directional one-to-many association?
Assume we have a BookDto with multiple reviews ...
1
vote
1
answer
67
views
I am getting java.lang.ClassNotFoundException: Cannot find implementation for com.example.demo.mapper.DoctorMapper
This is a mapper interface for mapping:
@Mapper
public interface DoctorMapper {
DoctorMapper INSTANCE = Mappers.getMapper(DoctorMapper.class);
DoctorDto toDto(Doctor doctor);
}
This is a model ...
1
vote
1
answer
31
views
How to put @beanmapping ignored by default to false from JDL
I'm trying to generate my entities and dtos from a JDL file, and I need that mapper files put @BeanMapping(ignoreByDefault=false) for recovering all the properties of a relationship's dto and not only ...
0
votes
1
answer
54
views
Mapstruct does not automatically map the base type constructor
environment:
java: 17.0.1
mapstruct: 1.4.2.Final
example:
public record RecordId(Long id) {
}
mapper
@Mapper(componentModel = "spring")
public interface RecordIdAssembler {
@Mapping(...
1
vote
1
answer
37
views
MapStruct passing in multiple objects error
I have trying to pass in two Entities as parameters to MapStruct but when I do I get compile error Method has no source parameter named "bird". Method source parameters are: "...
0
votes
1
answer
37
views
How to use mapstruct to map field from outside a collection to inside a collection
My Environment
java: 17.0.3
mapstruct: 1.4.2.Final
Here is my example
@Data
public class Example {
private Long outsideId;
private List<Inside> insides;
}
@Data
public class Inside {
...
0
votes
0
answers
90
views
Mapstruct: Can't generate mapping method from iterable to non-iterable type
I have following class structure:
SOURCE
class c1 {
List<c11> o1;
}
class c11 {
List<c12> o2
}
TARGET
class c1 {
List<c11> o1;
}
class c11 {
c12 o2
}
Here is mapper class:
@...
0
votes
1
answer
41
views
MapStruct @AfterMapping scope
With MapStruct does @AfterMapping have to apply to every method on the class?
Should each EntityDTO have its own mapper class?
1
vote
2
answers
51
views
Avoid DRY in @AfterMapping
I have two MapStruct Mapping classes for the same Bird entity, with Methods (customCodeBirdMapping) that do almost the same thing but have slightly different return types.
BirdViewMapper
@Mapper(...
0
votes
1
answer
51
views
using mapper in another mapper method
I want to use the mapper, in another mapper in the named method, how can I do this without using
Mappers.getMapper() ?
Exmaple:
@Mapper(componentModel = "spring", nullValueCheckStrategy = ...
0
votes
0
answers
50
views
Mapstruct ambiguous mapping methods - Spring 2.5.x
in version 2.5.x of Spring-Boot a method of the JPARepository has been deprecated in favor of another one (see here, section "Spring Data JPA")
Deprecated: MyObject getOne(Id id);
New one: ...
0
votes
1
answer
40
views
using mapstruct with service
I have the following Mapper:
@Mapper(componentModel = "spring",
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface StudentMapper {
...
0
votes
2
answers
117
views
Java: Deserialize object with list of Objects
I have this JSON: (passed as Map<String, Object>)
{
"id": 1000,
"lab": [
"LAB1",
"LAB2",
"LAB3"
],
"name": "...
0
votes
2
answers
91
views
How to get element of List in target(for mapping) if source doesn't contain this list?
I'm trying to map next structure(succesfully map in dozer, have some problems with mapstruct)
list is absent in source, how can I get its first element in mapper?
Target class:
@Data
public class ...
3
votes
2
answers
80
views
Model Mapper - using custom methods
A springboot project where I need to construct a DTO for a dashboard view using nominated fields from the parent and nominated fields from the newest of each of the children.
The entities are Plane ...
0
votes
2
answers
81
views
I'm getting error when i tried List<Entity> to Dto with MapStruct
public class Account {
private String accountId
private List<Transaction> transactions = new ArrayList<>();
public abstract class Transaction {
private double amount;
public ...
0
votes
1
answer
48
views
Mapstruct one to many ,fill DTO row by row
There is the Customer entity which has a one to many relationship to entity Address,so there is a List<Address> addresses=new ArrayList<> in Customer.Entity Address has an Email field.
For ...
0
votes
1
answer
84
views
SpringBoot Could not autowire. No beans of 'UserMapper' type found error for Mapstruct Mappers
This is my User class.
@Entity
@Table(name = "user_table")
@Data
public class User {
@Id
private Long id;
private String userName;
private String password;
}
This is my UserDto, I know ...
0
votes
2
answers
54
views
Mapstruct: custom mapping int to CharSeq Java
I am writing a mapper using MapStruct with Spring Boot and I currently am struggling with a mapping to map an int field to a CharSeq field:
I tried the following:
@Mapping(
target = "target....
2
votes
1
answer
89
views
How to use multiple properties from an object and map it to a single one in an other?
I need to map two objects together using both custom and regular mapping. Some properties are explicit and don't need any modifications, just mapping (because they don't have the same name), but some ...
0
votes
0
answers
131
views
Mapstruct: Mapping a dto in a dto to an entity in an entity
Mapstruct is not using a mapper declared in the "uses" definition in the @Mapper Annotation
I expected the productCategory and productPrices to be mapped by the mappers: ProductPricesMapper....
1
vote
3
answers
118
views
How to mock mapstruct mapper output in a Spring Service?
I have a spring service (MyService) that uses a mapstruct mapper (CustomMapstructMapper) :
@Service
public class MyService {
private final ClassA classA;
private final ClassB classB;
/*
...
0
votes
1
answer
169
views
MapStruct ignore all unmapped properties for specific method
There are several ways to ignore unmapped target properties in mapstruct.
We could list all properties to ignore for specific method:
@Mapping(target = "propName", ignore = true)
Or ...
0
votes
1
answer
126
views
No method found annotated with @Named#value on upper class
i'm using mapstruct with bellow dependency:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<...
0
votes
1
answer
42
views
Map nullable to several properties and specify default value for each
I try to map an enum (may be null) to a bean with 2 properties.
There is a mapping from the (source) enum to either properties and I have a default value for either property in the case the (source) ...
0
votes
1
answer
54
views
Map fields with the same name using Lombok
I'm using Lombok in a Spring Boot application, The MongoDB object are the same as my domain model so when I do the mapping I have to specify all the field names even they are the same
public static ...
1
vote
1
answer
37
views
How to instruct MapStruct to use Builder without a static method
I have a simple MapStruct class:
@Mapper
public interface SomeMapper {
SomeDto map(SomeModel someModel);
SomeModel map(SomeDto someDto);
}
The problem is, my SomeModel is using a Builder but ...
0
votes
2
answers
48
views
Map several fields to List with a mapstruct
for example, I have following entity:
class Bank
{
String name;
String employee1;
String employee2;
}
And a Dto object:
class BankDto
{
String name;
List<String> employeeList;
}
Is there a ...