File tree Expand file tree Collapse file tree 4 files changed +74
-0
lines changed
mapstruct-3/src/main/java/com/baeldung/fixingbeancreationissues Expand file tree Collapse file tree 4 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .fixingbeancreationissues .dto ;
2+
3+ public class PersonDto {
4+ private String name ;
5+ private int age ;
6+
7+ public PersonDto () {}
8+
9+ public PersonDto (String name , int age ) {
10+ this .name = name ;
11+ this .age = age ;
12+ }
13+
14+ public String getName () { return name ; }
15+ public void setName (String name ) { this .name = name ; }
16+
17+ public int getAge () { return age ; }
18+ public void setAge (int age ) { this .age = age ; }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .fixingbeancreationissues .mapper ;
2+
3+ import org .mapstruct .Mapper ;
4+ import com .baeldung .fixingbeancreationissues .model .Person ;
5+ import com .baeldung .fixingbeancreationissues .dto .PersonDto ;
6+
7+ // @Mapper {Incorrect}
8+ //@Mapper(componentModel = "spring") {Correct}
9+ public interface PersonMapper {
10+ PersonDto toDto (Person person );
11+ Person toEntity (PersonDto dto );
12+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .fixingbeancreationissues .model ;
2+
3+ public class Person {
4+ private String name ;
5+ private int age ;
6+
7+ public Person () {}
8+
9+ public Person (String name , int age ) {
10+ this .name = name ;
11+ this .age = age ;
12+ }
13+
14+ public String getName () { return name ; }
15+ public void setName (String name ) { this .name = name ; }
16+
17+ public int getAge () { return age ; }
18+ public void setAge (int age ) { this .age = age ; }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .fixingbeancreationissues .service ;
2+ import com .baeldung .fixingbeancreationissues .dto .PersonDto ;
3+ import com .baeldung .fixingbeancreationissues .mapper .PersonMapper ;
4+ import com .baeldung .fixingbeancreationissues .model .Person ;
5+
6+
7+ //@Service
8+
9+ public class PersonService {
10+
11+ private final PersonMapper personMapper ;
12+
13+ public PersonService (PersonMapper personMapper ) {
14+ this .personMapper = personMapper ;
15+ }
16+
17+ public PersonDto convertToDto (Person person ) {
18+ return personMapper .toDto (person );
19+ }
20+
21+ public Person convertToEntity (PersonDto dto ) {
22+ return personMapper .toEntity (dto );
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments