0

I'm quite new to Spring (Boot, latest version) and I have a problem with a mapper in a REST service. I have some UserMapper which maps, for instance, model user object (UserOP) to logic user object (User).

Users have an Address and I have to map it along with the user. The problem is, I don't succeed in injecting correctly my AddressMapper in the UserMapper.

I tryied the following but mapperAddress is null (Spring compiles, so I guess it just does not do the part "mapperAddress = new AddressMapper()"). Can someone explain me why and how to do it properly ? I searched on the Internet but did not understand.

@Component public class UserMapper {

   public UserMapper() {
       super();
   }

   @Autowired
   private AddressMapper mapperAddress;

   public User modelToIo(UserOP userOp){
       User user = null;
       if (userOp != null){
           User = new User();
           user.setAddress(mapperAddress.modelToIo(userOp.getAddress()));
           ...
       }
   }
   ...
}

and

@Component
public class AddressMapper {

    public AddressMapper() {
        super();
    }
    ...
}

Thanks.

lrosique
  • 113
  • 1
  • 9
  • Please show how you retrieve and use these beans. – Sotirios Delimanolis Jul 15 '16 at 16:19
  • In UserLogic (annoted by @Component), I have `@Autowired private UserMapper mapperUser` and then in some methods I have `public List listOfUsers() { List listUsers = mapperUser.modelToIoList(userRepository.findAll());`. But to test and tell you that AddressMapper is null when I call it in UserMapper, I use instead (in UserLogic) `private UserMapper mapperUser = new UserMapper();`. – lrosique Jul 15 '16 at 22:40
  • Put that in your question. But, yeah, your question is answered in the linked duplicate. – Sotirios Delimanolis Jul 15 '16 at 22:49

0 Answers0