java - Spring CrudRepository .orElseThrow() -
what proper way throw exception if database query returns empty? i'm trying utilize .orelsethrow()
method won't compile :
meeting meeting = meetingrepository.findbymeetingid(meetingid).orelsethrow(new meetingdoesnotexistexception(meetingid));
the compiler saying :
"he method orelsethrow(supplier) in type optional not applicable arguments (meetingrestcontroller.meetingdoesnotexistexception)
is possible lambda expressions?
crudrepository :
import java.util.optional; import org.springframework.data.repository.crudrepository; public interface meetingrepository extends crudrepository<meeting, long>{ optional<meeting> findbymeetingid(long id); }
exception :
@responsestatus(httpstatus.conflict) // 409 class meetingdoesnotexistexception extends runtimeexception{ public meetingdoesnotexistexception(long meetingid){ super("meeting " + meetingid + " not exist."); } }
try passing lambda look of type supplier<meetingdoesnotexistexception>
:
meeting meeting = meetingrepository.findbymeetingid(meetingid) .orelsethrow(() -> new meetingdoesnotexistexception(meetingid));
java spring java-8 crud spring-boot
No comments:
Post a Comment