java 发送具有可完成 Future 的异常
java Send exception with completable Future
如果 CompletableFuture 代码中的字段为空,我必须发送异常:
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
.orElseThrow(CurrentDataNotFoundException::new));
}
我的想法是,如果 child 在这种情况下 lastName 有一个空字段,我必须抛出一个自定义异常,我不太确定我该如何实现。
我的想法是使用 thenAccept 方法并像这样发送异常:
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
.orElseThrow(CurrentDataNotFoundException::new))
}.thenAccept{
........................
}
ObjectMapperUtils Code:
public static <S, D> D map(final S entityToMap, Class<D> expectedClass) {
return modelMapper.map(entityToMap, expectedClass);
}
public class ChildDTO {
@NotEmpty
private String id;
@PassengerIdConstraint
private String ownerId;
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
private String nickName;
@ChildAgeConstraint
private Integer age;
@NotNull
private Gender gender;
@NotEmpty
private String language;
我必须评估数据库中的 lastName 是否为空我必须抛出异常。
有什么想法吗?
我下面的方法是假设 findAsyncById
方法 returns CompletableFuture<Optional<ChildDTO>>
。所以你可以使用过滤器来检查 lastName
是否为空,如果为空则使用 orElseThrow
抛出异常
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
// If child last name is empty then return empty optional
.filter(child->!child.getLastName())
// If optional is empty then throw exception
.orElseThrow(CurrentDataNotFoundException::new))
如果 CompletableFuture 代码中的字段为空,我必须发送异常:
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
.orElseThrow(CurrentDataNotFoundException::new));
}
我的想法是,如果 child 在这种情况下 lastName 有一个空字段,我必须抛出一个自定义异常,我不太确定我该如何实现。
我的想法是使用 thenAccept 方法并像这样发送异常:
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
.orElseThrow(CurrentDataNotFoundException::new))
}.thenAccept{
........................
}
ObjectMapperUtils Code:
public static <S, D> D map(final S entityToMap, Class<D> expectedClass) {
return modelMapper.map(entityToMap, expectedClass);
}
public class ChildDTO {
@NotEmpty
private String id;
@PassengerIdConstraint
private String ownerId;
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
private String nickName;
@ChildAgeConstraint
private Integer age;
@NotNull
private Gender gender;
@NotEmpty
private String language;
我必须评估数据库中的 lastName 是否为空我必须抛出异常。
有什么想法吗?
我下面的方法是假设 findAsyncById
方法 returns CompletableFuture<Optional<ChildDTO>>
。所以你可以使用过滤器来检查 lastName
是否为空,如果为空则使用 orElseThrow
public CompletableFuture<ChildDTO> findChild(@NotEmpty String id) {
return ChildRepository.findAsyncById(id)
.thenApply(optionalChild -> optionalChild
.map(Child -> ObjectMapperUtils.map(Child, ChildDTO.class))
// If child last name is empty then return empty optional
.filter(child->!child.getLastName())
// If optional is empty then throw exception
.orElseThrow(CurrentDataNotFoundException::new))