0

I've got the following method in my Service file:

public MappingModel getById(String id) {
        return repository.findById(id);
    }

and this is my repository:

public interface MappingRepository extends MongoRepository<MappingModel, String> {

    List<MappingModel> findByProject(String project);

}

while compiling I'm getting the following error:

incompatible types: java.util.Optional<com.xxx.xxx.xxx.model.MappingModel> cannot be converted to com.xxx.xxx.xxx.model.MappingModel

What type should I be returning from my Service? shouldn't be the Model itself (what I'm currently trying to return)?

Thanks

2 Answers 2

3

The type that you’re supposed to be returning is in the error message . findById() returns an Optional<MappingModel>, so you can either change the return type of your service to Optional<MappingModel> or you can handle the Optional in the service.

https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

Edit to provide questioner more information about Optionals:

As a programmer, you've probably had faced the dreaded NullPointerException thousands of times. A big problem with Java is that when you look at a parameter, it's really hard to tell if it can be null or if it's guaranteed to be there, so what usually ends up happening is that your entire code base is filled with null checks (this is especially bad with nested objects).

Optionals are a way of explicitly saying, this object has a possibility that it's null, you need to account for that in some way. This might seem like a small thing, but it's actually very powerful for several reasons.

  1. If you're consistent with your use of Optionals in your code base, then you can mostly eliminate the dreaded null-check. If you always use an optional when something could be null, then you are implying that everything that is not an optional, is not null, so any nullpointers that you get are a result of a logical error in your code (which you know that you need to fix).

  2. Optionals work really well with the stream API. This is too big a topic to cover here, but basically, many program flows are quite simple. If we have the item, we attempt to do a sequence of operations to it, but if it's not, then we don't or throw an exception. Try it out, you'll appreciate Optionals a lot more after you do. I think it leads to really readable code.

  3. Many packages support Optionals, so they might handle them for you. For example, if this object is present, then we will return it as part of a JSON response. Otherwise, we'll ignore it.

From a use-case perspective, think about what it feels like to work with Collections like Lists. We usually expect that the List is never null, just empty, so using them just feels nice. Optionals are somewhat like an extension of this (and many of the stream API methods are overloaded to handle both Collections and Optionals).

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure if you're familiar with Optionals, but if you need it, I can provide some more context about the use of Optionals.
It worked. Thanks. I'm not familiar with Optionals so I'd honestly appreciate it if you could provide more context
I added a bit more information. It's a pretty big topic and I think you'll appreciate it more when you write some code with it, but I tried to cover most of the bases.
1

When you use findById() of MongoRepository, it will give you an Optional<> type which means, that data either can be present in the database or not. So change the type like following

 public Optional<MappingModel> getById(String id) {
        return repository.findById(id);
    }

When you use this method in service implementation, you need to use,

public MappingModel getById(String id){
   if(repository.getById(id).isPresent())
   {
       MappingModel m= repository.getById(id).get();
       // then your usual logic
       return m;
   }
   throw new RuntimeException("Data not found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.