I’m studying clean architecture building a simple Spring Boot application.
Some of my classes are:
public class EClient implements Serializable, EClienteInterface {...}
public interface ClientRepository extends JpaRepository<EClient, String> {...}
And finnaly, the reason of my question:
@Override
public Optional<EClientInterface> findClient(String cpf) {
return repository.findById(cpf).map(eClient -> eClient);
}
Basically, I was forced to put a .map(eClient -> eClient), because my error message was:
- Required -> Optional<EClientInterface>
- Provided -> Optional<EClient>
To sum up, my questions are:
-
Why do I need the .map? Is it because Optinal only accpets the exact type and not implementations or subclasses?
-
Why does this map actually works? It looks redundant to me